Polysubstance and tr. completion

Analyze the association between polysubstance at admission and tr. compleiton longitudinally along treatments, accounting for irregular observations.

Author

Andrés González Santa Cruz

Published

May 27, 2024

Code
rm(list = ls()) 
unlink("proposal_grant_23_24_files", recursive=T)
#fuentes: 
#https://rpubs.com/georgy_makarov/897844
path<-paste0(getwd(),'/env')

#Sys.setenv(RETICULATE_PYTHON =  "")

#Sys.setenv(RETICULATE_PYTHON =  Sys.which("python"))

#reticulate::py_config()
#use_python(paste0(path,"/Scripts/python.exe"))

#Sys.setenv(LD_LIBRARY_PATH =  paste0(path,"/Lib"))
#Sys.setenv(LD_LIBRARY_PATH_64 =  paste0(path,"/Lib"))
#instalar paquetes de funcionalidades básicas para tener ubicaciones relativas y acceso a python (reticulate)
if(!require(reticulate)){install.packages("reticulate")}
Loading required package: reticulate
Warning: package 'reticulate' was built under R version 4.1.3
Code
if(!require(rstudioapi)){install.packages("rstudioapi")}
Loading required package: rstudioapi
Warning: package 'rstudioapi' was built under R version 4.1.3
Code
invisible("Create env")
#https://stackoverflow.com/questions/54043607/how-to-set-pyenv-python-for-reticulate
#Directory H:/Mi unidad/PERSONAL ANDRES/UCH_salud_publica/asignaturas/env is not a Python virtualenv
#virtualenv_create(envname  = path, packages = c("pip", "statsmodels", "matplotlib", "numpy", "pandas", "scipy"))
# "C:/Users/andre/anaconda3/python.exe" -m venv "H:/Mi unidad/PERSONAL ANDRES/UCH_salud_publica/asignaturas/9_Computacion_Estadistica/env"

#FUENTES:
#https://rstudio.github.io/reticulate/articles/versions.html
#Virtual environment functions are not supported on Windows (the use of conda environments is recommended on Windows).

invisible("Use environment")
#https://ugoproto.github.io/ugo_r_doc/pdf/reticulate.pdf


# tx  <- readLines(paste0(path,"/pyvenv.cfg"))
# tx[[1]] <- paste0("home = ",gsub('/', '\\', paste0(path,"/Scripts/python.exe"), fixed=T))
# tx[[3]] <- "version = 3.8.0"

#writeLines(tx, con=paste0(path,"/pyvenv.cfg"))

#H:/Mi unidad/PERSONAL ANDRES/UCH_salud_publica/asignaturas/env/Scripts/python.exe"
#use_virtualenv(path)

#usar entorno virtual ya creado
#información sobre entorno virtual
#py_discover_config()
#conda_python(envname =  "r-scrublet")


# FUENTES
#https://akrabat.com/creating-virtual-environments-with-pyenv/
#https://rstudio.github.io/reticulate/reference/install_python.html
#https://github.com/pyenv/pyenv/wiki#suggested-build-environment
#https://github.com/pyenv/pyenv
#https://stackoverflow.com/questions/56755156/reticulate-not-setting-python-path
#https://github.com/rstudio/reticulate/issues/291#issuecomment-437143751
#https://github.com/pyenv/pyenv
#https://github.com/pyenv-win/pyenv-win#installation
#https://stackoverflow.com/questions/52060867/how-to-use-pip-for-pyenv
#https://github.com/pyenv/pyenv/issues/2417
Code
!pyenv install -l | findstr 3.8
!pip install --upgrade pyenv-win
!env PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install
!env PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install 3.7.5
!pyenv build
Code
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

# `r format(Sys.time(),'%B %d, %Y')`

# Data import

load("an_grant_23_24.RData")

# List all of the objects names in RData:
#ls(.GlobalEnv)
#ls(new_environment)
#rm(new_environment)

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_


Packages


Database consolidation

We used Base_fiscalia_v15f_grant_23_24 instead of v16, because we wanted to capture more treatments from each people.

Code
invisible("Label variables")

#2023-07-19
attr(Base_fiscalia_v15f_grant_23_24$motivodeegreso_mod_imp_rec,"label") <- "Complete status of treatment (binary)"
attr(Base_fiscalia_v15f_grant_23_24$offense_after_adm,"label") <- "Committing an offense after admission (binary)"
attr(Base_fiscalia_v15f_grant_23_24$age_at_censor_date,"label") <- "Age at censorship"
attr(Base_fiscalia_v15f_grant_23_24$age_offending_imp,"label") <- "Age at offending"
attr(Base_fiscalia_v15f_grant_23_24$age_tr_comp_imp,"label") <- "Age at completing tr."

#attr(Base_fiscalia_v15f_grant_23_24$tr_modality,"label") <- "Treatment Modality"
attr(Base_fiscalia_v15f_grant_23_24$time_to_off_from_adm,"label") <- "Time to offense from admission"
attr(Base_fiscalia_v15f_grant_23_24$time_to_drop_from_adm,"label") <- "Time to dropout from admission"

#2023-07-31
attr(Base_fiscalia_v15f_grant_23_24$age_at_death,"label") <- "Age at death"
attr(Base_fiscalia_v15f_grant_23_24$date_death,"label") <- "Time to death from admission"
attr(Base_fiscalia_v15f_grant_23_24$event_death,"label") <- "Death from admission"

attr(Base_fiscalia_v15f_grant_23_24$event_comp,"label") <- "Event: tr.completion"
attr(Base_fiscalia_v15f_grant_23_24$event_offense,"label") <- "Event: offense"


attr(Base_fiscalia_v15f_grant_23_24$edad_al_ing_1,"label") <- "Age (admission to treatment)"
attr(Base_fiscalia_v15f_grant_23_24$sex,"label") <- "Sex"
attr(Base_fiscalia_v15f_grant_23_24$edad_ini_cons,"label") <- "Age of Onset of Substance Use"
attr(Base_fiscalia_v15f_grant_23_24$escolaridad_rec,"label") <- "Educational Attainment"
attr(Base_fiscalia_v15f_grant_23_24$sus_principal_mod,"label") <- "Primary Substance (admission to treatment)"
attr(Base_fiscalia_v15f_grant_23_24$freq_cons_sus_prin,"label") <- "Frequency of Substance Use (Primary Substance)"
attr(Base_fiscalia_v15f_grant_23_24$condicion_ocupacional_corr,"label") <- "Corrected Occupational Status (f)"
attr(Base_fiscalia_v15f_grant_23_24$policonsumo,"label") <- "Co-occurring Substance Use Disorders (Polysubstance use)"

#20203-07-17
attr(Base_fiscalia_v15f_grant_23_24$otras_sus1_mod,"label") <- "First additional substance at admission"
attr(Base_fiscalia_v15f_grant_23_24$otras_sus2_mod,"label") <- "Second additional substance at admission"
attr(Base_fiscalia_v15f_grant_23_24$otras_sus3_mod,"label") <- "Third additional substance at admission"

attr(Base_fiscalia_v15f_grant_23_24$num_hijos_mod_joel_bin,"label") <- "Number of Children (dichotomized)"
attr(Base_fiscalia_v15f_grant_23_24$tenencia_de_la_vivienda_mod,"label") <- "Housing Situation (Tenure Status)"
attr(Base_fiscalia_v15f_grant_23_24$macrozona,"label") <- "Macro Administrative Zone in Chile"
attr(Base_fiscalia_v15f_grant_23_24$n_off_vio,"label") <- "Violent Criminal Offenses (Pre-Treatment)"
attr(Base_fiscalia_v15f_grant_23_24$n_off_acq,"label") <- "Acquisitive Criminal Offenses (Pre-Treatment)"
attr(Base_fiscalia_v15f_grant_23_24$n_off_sud,"label") <- "Substance-Related Criminal Offenses (Pre-Treatment)"
attr(Base_fiscalia_v15f_grant_23_24$n_off_oth,"label") <- "Other Criminal Offenses (Pre-Treatment)"
attr(Base_fiscalia_v15f_grant_23_24$dg_cie_10_rec,"label") <- "Psychiatric Comorbidity (ICD-10)"
attr(Base_fiscalia_v15f_grant_23_24$dg_trs_cons_sus_or,"label") <- "SUD Severity (Dependence status)"
attr(Base_fiscalia_v15f_grant_23_24$clas_r,"label") <- "Urbanicity"
attr(Base_fiscalia_v15f_grant_23_24$porc_pobr,"label") <- "Percentage of people in poverty"
attr(Base_fiscalia_v15f_grant_23_24$sus_ini_mod_mvv,"label") <- "Primary Substance (initial diagnosis)"
attr(Base_fiscalia_v15f_grant_23_24$ano_nac_corr,"label") <- "Corrected birth year"
attr(Base_fiscalia_v15f_grant_23_24$con_quien_vive_joel,"label") <- "Cohabitation status (Recoded) (f)"
attr(Base_fiscalia_v15f_grant_23_24$fis_comorbidity_icd_10,"label") <- "Physical Comorbidity (ICD-10)"

attr(Base_fiscalia_v15f_grant_23_24$n_post_off_vio,"label") <- "Count of Violent Criminal Offenses (Post-Treatment)"
attr(Base_fiscalia_v15f_grant_23_24$n_post_off_acq,"label") <- "Count of Acquisitive Criminal Offenses (Post-Treatment)"
attr(Base_fiscalia_v15f_grant_23_24$n_post_off_sud,"label") <- "Count of Substance-Related Criminal Offenses (Post-Treatment)"
attr(Base_fiscalia_v15f_grant_23_24$n_post_off_oth,"label") <- "Count of Other Criminal Offenses (Post-Treatment)"
attr(Base_fiscalia_v15f_grant_23_24$n_post_off,"label") <- "Count of Post-treatment Offenses"

#2024-01-27
attr(Base_fiscalia_v15f_grant_23_24$compromiso_biopsicosocial,"label") <- "Biopsychosocial compromise"
attr(Base_fiscalia_v15f_grant_23_24$origen_ingreso_mod,"label") <- "Treatment Admission Motive"


Base_fiscalia_v15f_grant_23_24$condicion_ocupacional_corr24 <- 
  dplyr::if_else(Base_fiscalia_v15f_grant_23_24$condicion_ocupacional_corr=="Looking for a job for the first time","Unemployed", Base_fiscalia_v15f_grant_23_24$condicion_ocupacional_corr, missing=NA_character_)
Base_fiscalia_v15f_grant_23_24$condicion_ocupacional_corr24 <- 
  dplyr::if_else(Base_fiscalia_v15f_grant_23_24$condicion_ocupacional_corr=="No activity","Inactive", Base_fiscalia_v15f_grant_23_24$condicion_ocupacional_corr24, missing=NA_character_)
Base_fiscalia_v15f_grant_23_24$condicion_ocupacional_corr24 <- 
  dplyr::if_else(Base_fiscalia_v15f_grant_23_24$condicion_ocupacional_corr=="Not seeking for work","Inactive", Base_fiscalia_v15f_grant_23_24$condicion_ocupacional_corr24, missing=NA_character_)
#Si la persona No Trabajó, interesa saber si está Desocupada (“busca trabajo por primera vez” o “cesante”) o
#Inactiva (que corresponde al resto de la opciones, quehaceres del hogar, estudiando, jubilado, etc.)

Base_fiscalia_v15f_grant_23_24 <- Base_fiscalia_v15f_grant_23_24 %>%
  dplyr::mutate(freq_cons_sus_prin_ord = dplyr::case_when(
    freq_cons_sus_prin == "Less than 1 day a week" ~ "1. Less than 1 day a week",
    freq_cons_sus_prin == "1 day a week or more" ~ "2. 1 day a week or more",
    freq_cons_sus_prin == "2 to 3 days a week" ~ "3. 2 to 3 days a week",
    freq_cons_sus_prin == "4 to 6 days a week" ~ "4. 4 to 6 days a week",
    freq_cons_sus_prin == "Daily" ~ "5. Daily"
  )) %>%
  dplyr::mutate(clas_r= dplyr::case_when(clas_r=="Mixta"~"Mixed", clas_r=="Urbana"~"Urban",clas_r=="Rural"~"Rural", T~clas_r)) %>% 
dplyr::mutate(tenencia_de_la_vivienda_mod  = dplyr::case_when(clas_r=="Illegal Settlement"~"Others", T~tenencia_de_la_vivienda_mod)) %>% 
  #2024-04-30: Divide type of plan according to program
          dplyr::mutate(tipo_de_plan_2_mod= dplyr::case_when(as.character(tipo_de_plan_2)=="M-PR"~"WO residential", tipo_de_plan_2=="PG-PR"~ "GP residential",tipo_de_plan_2=="M-PAI"~ "WO intensive ambulatory",tipo_de_plan_2=="PG-PAI"~ "GP intensive ambulatory",grepl("PAB",tipo_de_plan_2)~"basic ambulatory", T~NA_character_)) %>% 
          dplyr::mutate(tipo_de_plan_2= dplyr::case_when(grepl("PR",tipo_de_plan_2)~"residential",grepl("PAB",tipo_de_plan_2)~"basic ambulatory",grepl("PAI",tipo_de_plan_2)~"intensive ambulatory", T~NA_character_))
   #%>% #janitor::tabyl(tipo_de_plan_2_mod)

attr(Base_fiscalia_v15f_grant_23_24$condicion_ocupacional_corr24,"label") <- "Corrected Occupational Status"
attr(Base_fiscalia_v15f_grant_23_24$freq_cons_sus_prin_ord,"label") <- "Frequency of Substance Use (Primary Substance)"

# Check the new variable
table(Base_fiscalia_v15f_grant_23_24$freq_cons_sus_prin_ord)

1. Less than 1 day a week   2. 1 day a week or more     3. 2 to 3 days a week 
                     4178                      5610                     23716 
    4. 4 to 6 days a week                  5. Daily 
                    13923                     37200 
Code
vars_cov <- c("motivodeegreso_mod_imp_rec", "edad_al_ing_1", "sex", "edad_ini_cons", "escolaridad_rec", "sus_principal_mod", "freq_cons_sus_prin_ord", "condicion_ocupacional_corr24", "policonsumo", "num_hijos_mod_joel_bin", "tenencia_de_la_vivienda_mod", "macrozona", "dg_cie_10_rec", "dg_trs_cons_sus_or", "clas_r", "porc_pobr", "sus_ini_mod_mvv", "ano_nac_corr", "con_quien_vive_joel", "fis_comorbidity_icd_10",  "compromiso_biopsicosocial", "origen_ingreso_mod", "tipo_centro_pub","tipo_de_plan_2", "tipo_de_plan_2_mod") 


#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_
invisible("Function to format CreateTableOne into a database")

as.data.frame.TableOne <- function(x, ...) {capture.output(print(x,
                          showAllLevels = TRUE, varLabels = T,...) -> x)
  y <- as.data.frame(x)
  y$characteristic <- dplyr::na_if(rownames(x), "")
  y <- y %>%
  fill(characteristic, .direction = "down") %>%
  dplyr::select(characteristic, everything())
  rownames(y) <- NULL
  y}
#_#_#_#_#_#_#_#_#_#_#_#_#_

tbone_desc_merge_total_grant_23_24<-
CreateTableOne(vars=c(setdiff(vars_cov, "policonsumo"), "offense_after_adm"), data=  Base_fiscalia_v15f_grant_23_24[,c("motivodeegreso_mod_imp_rec", "edad_al_ing_1", "sex", "edad_ini_cons", 
"escolaridad_rec", "sus_principal_mod", "freq_cons_sus_prin_ord", 
"condicion_ocupacional_corr24", "policonsumo", "num_hijos_mod_joel_bin", 
"tenencia_de_la_vivienda_mod", "macrozona", "dg_cie_10_rec", "dg_trs_cons_sus_or", 
"clas_r", "porc_pobr", "sus_ini_mod_mvv", "ano_nac_corr", "con_quien_vive_joel", 
"fis_comorbidity_icd_10", "compromiso_biopsicosocial", "origen_ingreso_mod","tipo_centro_pub")], factorVars = setdiff(vars_cov, c("motivodeegreso_mod_imp_rec","edad_al_ing_1", "edad_ini_cons","ano_nac_corr", "porc_pobr")), smd=T, strata="policonsumo", addOverall = T, includeNA=T, test=T)

Warning in ModuleReturnVarsExist(vars, data): The data frame does not have: tipo_de_plan_2 tipo_de_plan_2_mod offense_after_adm Dropped

Warning in ModuleReturnVarsExist(factorVars, data): The data frame does not have: tipo_de_plan_2 tipo_de_plan_2_mod Dropped

We compared the characteristics of people who used multiple substances and those who did not in the total database between 2013 and 2019.

Code
as.data.frame.TableOne(tbone_desc_merge_total_grant_23_24, smd=T, nonnormal= T)%>% 
  dplyr::mutate(char2=characteristic) %>% 
  tidyr::fill(char2) %>% 
  dplyr::select(char2,everything()) %>% 
  dplyr::mutate(level=ifelse(is.na(level),"[Missing]",level)) %>% 
  dplyr::mutate(char2=dplyr::case_when(characteristic=="NA"~NA_character_,TRUE~as.character(characteristic))) %>% 
  format_cells(1, 1:length(names(.)), "bold") %>%
  dplyr::select(-1) %>% 
  dplyr::mutate(across(.fns = ~ str_replace_all(., pattern = "\\( ", replacement = "\\("))) %>% 
  dplyr::mutate(across(.fns = ~ str_trim(.))) %>% 
  knitr::kable(size=10, format="markdown",caption= "Summary descriptives, Polysubstance(1) and no Polysubstance use (0)", escape=T)

fill: changed 56 values (71%) of ‘characteristic’ (56 fewer NA)

Summary descriptives, Polysubstance(1) and no Polysubstance use (0)
characteristic level Overall 0 1 p test SMD
n 85048 22552 62496
Complete status of treatment (binary) (%) Treatment completion 19278 (22.7) 6404 (28.4) 12874 (20.6) <0.001 0.183
Complete status of treatment (binary) (%) Treatment non-completion 65761 (77.3) 16148 (71.6) 49613 (79.4)
Complete status of treatment (binary) (%) [Missing] 9 (0.0) 0 (0.0) 9 (0.0)
Age (admission to treatment) (median [IQR]) 34.21 [27.41, 43.17] 41.71 [32.29, 51.21] 32.33 [26.45, 39.90] <0.001 nonnorm 0.743
Sex (%) Men 64028 (75.3) 16502 (73.2) 47526 (76.0) <0.001 0.066
Sex (%) Women 21020 (24.7) 6050 (26.8) 14970 (24.0)
Age of Onset of Substance Use (median [IQR]) 15.00 [14.00, 18.00] 17.00 [14.00, 20.00] 15.00 [13.00, 17.00] <0.001 nonnorm 0.425
Educational Attainment (%) 1-More than high school 14068 (16.5) 3215 (14.3) 10853 (17.4) <0.001 0.230
Educational Attainment (%) 2-Completed high school or less 46653 (54.9) 11143 (49.4) 35510 (56.8)
Educational Attainment (%) 3-Completed primary school or less 23943 (28.2) 8044 (35.7) 15899 (25.4)
Educational Attainment (%) [Missing] 384 (0.5) 150 (0.7) 234 (0.4)
Primary Substance (admission to treatment) (%) Alcohol 28859 (33.9) 14212 (63.0) 14647 (23.4) <0.001 0.890
Primary Substance (admission to treatment) (%) Cocaine hydrochloride 16151 (19.0) 2337 (10.4) 13814 (22.1)
Primary Substance (admission to treatment) (%) Cocaine paste 32681 (38.4) 4511 (20.0) 28170 (45.1)
Primary Substance (admission to treatment) (%) Marijuana 5771 (6.8) 972 (4.3) 4799 (7.7)
Primary Substance (admission to treatment) (%) Other 1585 (1.9) 519 (2.3) 1066 (1.7)
Primary Substance (admission to treatment) (%) [Missing] 1 (0.0) 1 (0.0) 0 (0.0)
Frequency of Substance Use (Primary Substance) (%) 1. Less than 1 day a week 4178 (4.9) 1530 (6.8) 2648 (4.2) <0.001 0.186
Frequency of Substance Use (Primary Substance) (%) 2. 1 day a week or more 5610 (6.6) 1922 (8.5) 3688 (5.9)
Frequency of Substance Use (Primary Substance) (%) 3. 2 to 3 days a week 23716 (27.9) 6641 (29.4) 17075 (27.3)
Frequency of Substance Use (Primary Substance) (%) 4. 4 to 6 days a week 13923 (16.4) 3580 (15.9) 10343 (16.5)
Frequency of Substance Use (Primary Substance) (%) 5. Daily 37200 (43.7) 8744 (38.8) 28456 (45.5)
Frequency of Substance Use (Primary Substance) (%) [Missing] 421 (0.5) 135 (0.6) 286 (0.5)
Corrected Occupational Status (%) Employed 41407 (48.7) 12657 (56.1) 28750 (46.0) <0.001 0.276
Corrected Occupational Status (%) Inactive 14258 (16.8) 4206 (18.7) 10052 (16.1)
Corrected Occupational Status (%) Unemployed 29382 (34.5) 5688 (25.2) 23694 (37.9)
Corrected Occupational Status (%) [Missing] 1 (0.0) 1 (0.0) 0 (0.0)
Number of Children (dichotomized) (%) 0 20138 (23.7) 4161 (18.5) 15977 (25.6) <0.001 0.172
Number of Children (dichotomized) (%) 1 64165 (75.4) 18191 (80.7) 45974 (73.6)
Number of Children (dichotomized) (%) [Missing] 745 (0.9) 200 (0.9) 545 (0.9)
tenencia_de_la_vivienda_mod (%) Illegal Settlement 906 (1.1) 200 (0.9) 706 (1.1) <0.001 0.267
tenencia_de_la_vivienda_mod (%) Others 2354 (2.8) 609 (2.7) 1745 (2.8)
tenencia_de_la_vivienda_mod (%) Owner/Transferred dwellings/Pays Dividends 29934 (35.2) 9863 (43.7) 20071 (32.1)
tenencia_de_la_vivienda_mod (%) Renting 14566 (17.1) 3835 (17.0) 10731 (17.2)
tenencia_de_la_vivienda_mod (%) Stays temporarily with a relative 32609 (38.3) 6829 (30.3) 25780 (41.3)
tenencia_de_la_vivienda_mod (%) [Missing] 4679 (5.5) 1216 (5.4) 3463 (5.5)
Macro Administrative Zone in Chile (%) Center 64341 (75.7) 16681 (74.0) 47660 (76.3) <0.001 0.293
Macro Administrative Zone in Chile (%) North 12151 (14.3) 2243 (9.9) 9908 (15.9)
Macro Administrative Zone in Chile (%) South 8536 (10.0) 3625 (16.1) 4911 (7.9)
Macro Administrative Zone in Chile (%) [Missing] 20 (0.0) 3 (0.0) 17 (0.0)
Psychiatric Comorbidity (ICD-10) (%) Without psychiatric comorbidity 32337 (38.0) 10022 (44.4) 22315 (35.7) <0.001 0.184
Psychiatric Comorbidity (ICD-10) (%) Diagnosis unknown (under study) 16099 (18.9) 3575 (15.9) 12524 (20.0)
Psychiatric Comorbidity (ICD-10) (%) With psychiatric comorbidity 36612 (43.0) 8955 (39.7) 27657 (44.3)
SUD Severity (Dependence status) (%) Drug dependence 61836 (72.7) 13974 (62.0) 47862 (76.6) <0.001 0.321
SUD Severity (Dependence status) (%) Hazardous consumption 23211 (27.3) 8577 (38.0) 14634 (23.4)
SUD Severity (Dependence status) (%) [Missing] 1 (0.0) 1 (0.0) 0 (0.0)
clas_r (%) Mixed 8205 (9.6) 2850 (12.6) 5355 (8.6) <0.001 0.272
clas_r (%) Rural 7148 (8.4) 2977 (13.2) 4171 (6.7)
clas_r (%) Urban 69693 (81.9) 16724 (74.2) 52969 (84.8)
clas_r (%) [Missing] 2 (0.0) 1 (0.0) 1 (0.0)
Percentage of people in poverty (median [IQR]) 0.11 [0.07, 0.16] 0.11 [0.07, 0.15] 0.12 [0.08, 0.17] <0.001 nonnorm 0.104
Primary Substance (initial diagnosis) (%) Alcohol 46623 (54.8) 15574 (69.1) 31049 (49.7) <0.001 0.505
Primary Substance (initial diagnosis) (%) Cocaine hydrochloride 3162 (3.7) 957 (4.2) 2205 (3.5)
Primary Substance (initial diagnosis) (%) Cocaine paste 3864 (4.5) 1321 (5.9) 2543 (4.1)
Primary Substance (initial diagnosis) (%) Marijuana 22991 (27.0) 3083 (13.7) 19908 (31.9)
Primary Substance (initial diagnosis) (%) Other 1987 (2.3) 563 (2.5) 1424 (2.3)
Primary Substance (initial diagnosis) (%) [Missing] 6421 (7.5) 1054 (4.7) 5367 (8.6)
Corrected birth year (median [IQR]) 1980.00 [1971.00, 1987.00] 1974.00 [1964.00, 1983.00] 1982.00 [1974.00, 1988.00] <0.001 nonnorm 0.630
Cohabitation status (Recoded) (f) (%) Alone 8026 (9.4) 2765 (12.3) 5261 (8.4) <0.001 0.342
Cohabitation status (Recoded) (f) (%) Family of origin 35576 (41.8) 6866 (30.4) 28710 (45.9)
Cohabitation status (Recoded) (f) (%) Others 7291 (8.6) 1802 (8.0) 5489 (8.8)
Cohabitation status (Recoded) (f) (%) With couple/children 34154 (40.2) 11118 (49.3) 23036 (36.9)
Cohabitation status (Recoded) (f) (%) [Missing] 1 (0.0) 1 (0.0) 0 (0.0)
Physical Comorbidity (ICD-10) (%) Without physical comorbidity 33609 (39.5) 8363 (37.1) 25246 (40.4) <0.001 0.084
Physical Comorbidity (ICD-10) (%) Diagnosis unknown (under study) 45892 (54.0) 12457 (55.2) 33435 (53.5)
Physical Comorbidity (ICD-10) (%) One or more 5547 (6.5) 1732 (7.7) 3815 (6.1)
Biopsychosocial compromise (%) 1-Mild 7986 (9.4) 3801 (16.9) 4185 (6.7) <0.001 0.420
Biopsychosocial compromise (%) 2-Moderate 48355 (56.9) 13544 (60.1) 34811 (55.7)
Biopsychosocial compromise (%) 3-Severe 27150 (31.9) 4743 (21.0) 22407 (35.9)
Biopsychosocial compromise (%) [Missing] 1557 (1.8) 464 (2.1) 1093 (1.7)
Treatment Admission Motive (%) Spontaneous 38028 (44.7) 10097 (44.8) 27931 (44.7) <0.001 0.187
Treatment Admission Motive (%) Assisted Referral 7967 (9.4) 1455 (6.5) 6512 (10.4)
Treatment Admission Motive (%) Other 4514 (5.3) 1184 (5.3) 3330 (5.3)
Treatment Admission Motive (%) Justice Sector 7976 (9.4) 2787 (12.4) 5189 (8.3)
Treatment Admission Motive (%) Health Sector 26563 (31.2) 7029 (31.2) 19534 (31.3)
tipo_centro_pub (%) FALSE 24070 (28.3) 3414 (15.1) 20656 (33.1) <0.001 0.429
tipo_centro_pub (%) TRUE 60958 (71.7) 19135 (84.8) 41823 (66.9)
tipo_centro_pub (%) [Missing] 20 (0.0) 3 (0.0) 17 (0.0)
Code
#kable(size=10, format="html",caption= "Summary descriptives, by Baseline Treatment Status") %>%     kableExtra::kable_classic()

as.data.frame.TableOne(tbone_desc_merge_total_grant_23_24, smd=T, nonnormal= T)%>% 
  dplyr::mutate(char2=characteristic) %>% 
  tidyr::fill(char2) %>% 
  dplyr::select(char2,everything()) %>% 
  dplyr::mutate(level=ifelse(is.na(level),"[Missing]",level)) %>% 
  dplyr::mutate(char2=dplyr::case_when(characteristic=="NA"~NA_character_,TRUE~as.character(characteristic))) %>% 
  format_cells(1, 1:length(names(.)), "bold") %>%
  dplyr::select(-1) %>% 
  dplyr::mutate(across(.fns = ~ str_replace_all(., pattern = "\\( ", replacement = "\\("))) %>% 
  dplyr::mutate(across(.fns = ~ str_trim(.))) %>% 
  write.table("_proposal_grant/2023/baseline_psu_desc_smd.csv", dec=",", sep="\t")

fill: changed 56 values (71%) of ‘characteristic’ (56 fewer NA)

Code
#C:\Users\CISS Fondecyt\Mi unidad\Alvacast\SISTRAT 2022 (github)\_proposal_grant/2023

Long format

Code
CONS_C1_df_dup_SEP_2020_mod<-
CONS_C1_df_dup_SEP_2020[,c("hash_key","rn_hash","otras_sus1_mod","otras_sus2_mod","otras_sus3_mod","sus_principal_mod")] %>% 
    dplyr::mutate(across(starts_with("otras_sus"), ~ dplyr::case_when(
        as.character(.) == "Cocaína" ~ "Cocaine hydrochloride",
        as.character(.) == "Marihuana" ~ "Marijuana",
        as.character(.) == "Otros" ~ "Other",
        as.character(.) == "Pasta Base" ~ "Cocaine paste",
        TRUE ~ as.character(.)
    ))) %>%
    dplyr::mutate(sus_principal_mod= as.character(sus_principal_mod)) %>%
  # Replace with missing if there are any duplicates
  dplyr::mutate(otras_sus3_mod= dplyr::case_when(otras_sus3_mod==otras_sus2_mod| otras_sus3_mod==otras_sus1_mod| otras_sus3_mod==sus_principal_mod~ NA_character_, TRUE~ otras_sus3_mod)) %>% 
  dplyr::mutate(otras_sus2_mod= dplyr::case_when(otras_sus2_mod==otras_sus1_mod| otras_sus2_mod== sus_principal_mod~ NA_character_, TRUE~ otras_sus2_mod)) %>% 
  dplyr::mutate(otras_sus1_mod= dplyr::case_when(otras_sus1_mod== sus_principal_mod~ NA_character_, TRUE~ otras_sus1_mod)) %>% 
  # Empty substances were replaced with the previous
  dplyr::mutate(otras_sus2_mod= dplyr::case_when(!is.na(otras_sus3_mod) & is.na(otras_sus2_mod)~ otras_sus3_mod, TRUE~ otras_sus2_mod)) %>% 
  dplyr::mutate(otras_sus1_mod= dplyr::case_when(!is.na(otras_sus2_mod) & is.na(otras_sus1_mod)~ otras_sus2_mod, TRUE~ otras_sus1_mod)) %>%
  dplyr::mutate(sus_principal_mod= dplyr::case_when(!is.na(otras_sus1_mod) & is.na(sus_principal_mod)~ otras_sus1_mod, TRUE~ sus_principal_mod)) %>%  
  # Replace with missing if there are any duplicates
  dplyr::mutate(otras_sus3_mod= dplyr::case_when(otras_sus3_mod==otras_sus2_mod| otras_sus3_mod==otras_sus1_mod| otras_sus3_mod==sus_principal_mod~ NA_character_, TRUE~ otras_sus3_mod)) %>% 
  dplyr::mutate(otras_sus2_mod= dplyr::case_when(otras_sus2_mod==otras_sus1_mod| otras_sus2_mod== sus_principal_mod~ NA_character_, TRUE~ otras_sus2_mod)) %>% 
  dplyr::mutate(otras_sus1_mod= dplyr::case_when(otras_sus1_mod== sus_principal_mod~ NA_character_, TRUE~ otras_sus1_mod)) %>%   
    # Replace with missing if there are any duplicates
  dplyr::mutate(otras_sus3_mod= dplyr::case_when(otras_sus3_mod==otras_sus2_mod| otras_sus3_mod==otras_sus1_mod| otras_sus3_mod==sus_principal_mod~ NA_character_, TRUE~ otras_sus3_mod)) %>% 
  dplyr::mutate(otras_sus2_mod= dplyr::case_when(otras_sus2_mod==otras_sus1_mod| otras_sus2_mod== sus_principal_mod~ NA_character_, TRUE~ otras_sus2_mod)) %>% 
  dplyr::mutate(otras_sus1_mod= dplyr::case_when(otras_sus1_mod== sus_principal_mod~ NA_character_, TRUE~ otras_sus1_mod)) %>%  
  dplyr::select(hash_key, rn_hash, sus_principal_mod,otras_sus1_mod) %>% 
  dplyr::mutate(policonsumo2= ifelse(!is.na(otras_sus1_mod),1,0)) %>% 
#  dplyr::mutate(adv=ifelse(otras_sus1_mod==sus_principal_mod,1,0)) %>% 
#    dplyr::filter(adv==1)
  data.table::data.table()
Code
# vars_cov <- c("motivodeegreso_mod_imp_rec", "edad_al_ing_1", "sex", "edad_ini_cons", "escolaridad_rec", "sus_principal_mod", "freq_cons_sus_prin", "condicion_ocupacional_corr", "policonsumo", "otras_sus1_mod", "otras_sus2_mod", "otras_sus3_mod", "num_hijos_mod_joel_bin", "tenencia_de_la_vivienda_mod", "macrozona", "n_off_vio", "n_off_acq", "n_off_sud", "n_off_oth", "dg_cie_10_rec", "dg_trs_cons_sus_or", "clas_r", "porc_pobr", "sus_ini_mod_mvv", "ano_nac_corr", "con_quien_vive_joel", "fis_comorbidity_icd_10", "time_to_off_from_adm", "time_to_drop_from_adm", "age_at_censor_date", "age_tr_comp_imp", "age_offending_imp", "age_at_death","event_death", "n_post_off_vio", "n_post_off_acq", "n_post_off_sud", "n_post_off_oth", "n_post_off","hurto", "robo", "venta_drogas", "rina", "total_vif", "otro", "tot_off_top", "edad_a_ap_top_num",   "edad_b_ap_top_num", "na_top_count", "n_top_records")

#tipo_de_plan_2_1
#motivodeegreso_mod_imp_1
#dias_treat_imp_sin_na_1
#fech_ing_num_1
#fech_egres_num_1
#edad_al_ing_1
#edad_al_egres_1

Base_fiscalia_v15f_grant_23_24_long<-
Base_fiscalia_v15f_grant_23_24 %>% 
  dplyr::select("hash_key",
                paste0("motivodeegreso_mod_imp_",1:10),
                paste0("dias_treat_imp_sin_na_",1:10),
                paste0("fech_ing_num_",1:10),
                paste0("fech_egres_num_",1:10),
                paste0("tipo_de_plan_2_",1:10),
                c("edad_al_ing_1", "sex", "edad_ini_cons", "escolaridad_rec", "sus_principal_mod", "freq_cons_sus_prin_ord", "condicion_ocupacional_corr24", "policonsumo", "num_hijos_mod_joel_bin", "tenencia_de_la_vivienda_mod", "macrozona", "n_off_vio", "n_off_acq", "n_off_sud", "n_off_oth", "dg_cie_10_rec", "dg_trs_cons_sus_or", "clas_r", "porc_pobr", "sus_ini_mod_mvv", "ano_nac_corr", "con_quien_vive_joel", "tipo_de_plan_2_mod", 
                  #2024-01-28: se agregan 3 variables
                  "tipo_centro_pub", "origen_ingreso_mod", "compromiso_biopsicosocial")) %>% 
  #has 75 missing
        dplyr::mutate(across(paste0("tipo_de_plan_2_",1:10),~dplyr::case_when(grepl("PR",.)~"residential",grepl("PAB",.)~"basic ambulatory",grepl("PAI",.)~"intensive ambulatory", T~NA_character_))) %>% 
# Pivot longer
  pivot_longer(
    # 2024-01-27: por lo que entiendo, estas variables se excluyen de la transformación longitudinal. ES NECESARIO LONGITUDINALIZARLAS
    cols = -c(hash_key, edad_al_ing_1, sex, edad_ini_cons, escolaridad_rec, sus_principal_mod, freq_cons_sus_prin_ord, condicion_ocupacional_corr24, policonsumo, num_hijos_mod_joel_bin, tenencia_de_la_vivienda_mod, macrozona, dg_cie_10_rec, dg_trs_cons_sus_or, clas_r, porc_pobr, sus_ini_mod_mvv, ano_nac_corr, con_quien_vive_joel, tipo_de_plan_2_mod,
             #2024-01-28: se agregan 3 variables
                 tipo_centro_pub, origen_ingreso_mod, compromiso_biopsicosocial 
              ),
    names_to = c(".value", "treatment"),
    names_pattern = "(.+)_(\\d+)$"
  ) %>%
  dplyr::mutate(treatment=as.numeric(treatment)) %>% 
  dplyr::filter(!is.na(fech_ing_num)) %>% 
# create event variables
  arrange(hash_key, treatment) %>% 
  dplyr::group_by(hash_key) %>% 
  dplyr::mutate(enter_time=min(fech_ing_num, na.rm=T)) %>%
  dplyr::mutate(time=fech_egres_num-enter_time) %>%
  dplyr::mutate(cens_time= as.numeric(as.Date("2019-11-13"))-enter_time) %>% 
  dplyr::ungroup() %>% 
  #define event: dropout
  dplyr::mutate(event=ifelse(motivodeegreso_mod_imp %in% c("Abandono Tardio", "Abandono Temprano", "Alta Administrativa"),1,0)) %>% 
  #censorship time if event not present
    dplyr::mutate(time= dplyr::case_when(event==1~ cens_time, TRUE~ time)) %>%   #to months
  dplyr::mutate(time=time/30.5, cens_time= cens_time/30.5) %>% 
  #id variable
  group_by(hash_key) %>%
  mutate(is_first_occurrence = if_else(row_number() == 1, 1, 0)) %>%
  ungroup() %>%
  mutate(id = cumsum(is_first_occurrence)) %>% 
  dplyr::select(-is_first_occurrence)

pivot_longer: reorganized (motivodeegreso_mod_imp_1, motivodeegreso_mod_imp_2, motivodeegreso_mod_imp_3, motivodeegreso_mod_imp_4, motivodeegreso_mod_imp_5, …) into (treatment, motivodeegreso_mod_imp, dias_treat_imp_sin_na, fech_ing_num, fech_egres_num, …) [was 85048x77, now 1190672x29]

Code
#  length(unique(Base_fiscalia_v15f_grant_23_24_long2$hash_key))
# [1] 72404

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#Added the time-varyinng polyuse
Base_fiscalia_v15f_grant_23_24_long2<-
Base_fiscalia_v15f_grant_23_24_long %>% 
  dplyr::left_join(CONS_C1_df_dup_SEP_2020_mod[,c("hash_key",
#2024-01-28: agregué biopsychosocial_comp
"rn_hash","policonsumo2")], by=c(c("hash_key"="hash_key","treatment"="rn_hash"))) %>% # nrow() 109756
  dplyr::left_join(CONS_C1_df_dup_SEP_2020[,c("hash_key",
#2024-01-28: agregué biopsychosocial_comp
"rn_hash","compromiso_biopsicosocial")], by=c(c("hash_key"="hash_key","treatment"="rn_hash"))) %>% # nrow() 109756
  dplyr::rename("compromiso_biopsicosocial"="compromiso_biopsicosocial.x") %>% 
#2024-01-29: filtered treatments without finish
  dplyr::filter(!motivodeegreso_mod_imp %in% c("En curso", "Muerte", "Derivación")) %>% 
  #dplyr::select(hash_key,fech_ing_num, fech_egres_num, time, enter_time, cens_time)
  #2024-01-29: create event variables, again
  arrange(hash_key, treatment) %>% 
  dplyr::group_by(hash_key) %>% 
  dplyr::mutate(treatment=row_number()) %>% 
  dplyr::mutate(max_treatment= max( treatment,na.rm=T)) %>% 
  dplyr::mutate(enter_time=min(fech_ing_num, na.rm=T)) %>%
  dplyr::mutate(cens_time= as.numeric(as.Date("2019-11-13"))-enter_time) %>% 
  dplyr::ungroup() %>% 
  #2024-03-04: move out of the nested de discharge date
  dplyr::mutate(time= fech_egres_num-enter_time) %>%
  dplyr::mutate(time=time/30.5, cens_time= cens_time/30.5) %>% 
  dplyr::group_by(hash_key) %>% 
  dplyr::mutate(lag_time= lag(time)) %>% 
  dplyr::ungroup() %>%   
  dplyr::mutate(time=dplyr::case_when(lag_time==time & treatment==2~time+.001,
                                 lag_time==time & treatment==3~time+.001,
                                 lag_time==time & treatment==4~time+.001,
                                 lag_time==time & treatment==5~time+.001,
                                 lag_time==time & treatment==6~time+.001,
                                 lag_time==time & treatment==7~time+.001,
                                 lag_time==time & treatment==8~time+.001,
                                 lag_time==time & treatment==9~time+.001,
                                 TRUE~time)) %>% 
  dplyr::mutate(time=dplyr::case_when(lag_time==time & treatment==2~time+.001,
                                 lag_time==time & treatment==3~time+.001,
                                 lag_time==time & treatment==4~time+.001,
                                 lag_time==time & treatment==5~time+.001,
                                 lag_time==time & treatment==6~time+.001,
                                 lag_time==time & treatment==7~time+.001,
                                 lag_time==time & treatment==8~time+.001,
                                 lag_time==time & treatment==9~time+.001,
                                 TRUE~time)) %>% 
  dplyr::mutate(time=dplyr::case_when(lag_time==time & treatment==2~time+.001,
                                 lag_time==time & treatment==3~time+.001,
                                 lag_time==time & treatment==4~time+.001,
                                 lag_time==time & treatment==5~time+.001,
                                 lag_time==time & treatment==6~time+.001,
                                 lag_time==time & treatment==7~time+.001,
                                 lag_time==time & treatment==8~time+.001,
                                 lag_time==time & treatment==9~time+.001,
                                 TRUE~time)) %>% 
  dplyr::mutate(time=dplyr::case_when(lag_time==time & treatment==2~time+.001,
                                 lag_time==time & treatment==3~time+.001,
                                 lag_time==time & treatment==4~time+.001,
                                 lag_time==time & treatment==5~time+.001,
                                 lag_time==time & treatment==6~time+.001,
                                 lag_time==time & treatment==7~time+.001,
                                 lag_time==time & treatment==8~time+.001,
                                 lag_time==time & treatment==9~time+.001,
                                 TRUE~time)) %>% 
  dplyr::mutate(time=dplyr::case_when(lag_time==time & treatment==2~time+.001,
                                 lag_time==time & treatment==3~time+.001,
                                 lag_time==time & treatment==4~time+.001,
                                 lag_time==time & treatment==5~time+.001,
                                 lag_time==time & treatment==6~time+.001,
                                 lag_time==time & treatment==7~time+.001,
                                 lag_time==time & treatment==8~time+.001,
                                 lag_time==time & treatment==9~time+.001,
                                 TRUE~time)) %>% 
  dplyr::mutate(time=dplyr::case_when(lag_time==time & treatment==2~time+.001,
                                 lag_time==time & treatment==3~time+.001,
                                 lag_time==time & treatment==4~time+.001,
                                 lag_time==time & treatment==5~time+.001,
                                 lag_time==time & treatment==6~time+.001,
                                 lag_time==time & treatment==7~time+.001,
                                 lag_time==time & treatment==8~time+.001,
                                 lag_time==time & treatment==9~time+.001,
                                 TRUE~time)) %>% 
  dplyr::mutate(time=dplyr::case_when(lag_time==time & treatment==2~time+.001,
                                 lag_time==time & treatment==3~time+.001,
                                 lag_time==time & treatment==4~time+.001,
                                 lag_time==time & treatment==5~time+.001,
                                 lag_time==time & treatment==6~time+.001,
                                 lag_time==time & treatment==7~time+.001,
                                 lag_time==time & treatment==8~time+.001,
                                 lag_time==time & treatment==9~time+.001,
                                 TRUE~time)) %>% 
  dplyr::mutate(time=dplyr::case_when(lag_time==time & treatment==2~time+.001,
                                 lag_time==time & treatment==3~time+.001,
                                 lag_time==time & treatment==4~time+.001,
                                 lag_time==time & treatment==5~time+.001,
                                 lag_time==time & treatment==6~time+.001,
                                 lag_time==time & treatment==7~time+.001,
                                 lag_time==time & treatment==8~time+.001,
                                 lag_time==time & treatment==9~time+.001,
                                 TRUE~time)) %>% 
    #2024-01-29: censorship time if event not present
  #2024-03-04: surv_time in other variable
  dplyr::mutate(surv_time= dplyr::case_when(event==0~ cens_time, TRUE~ time))#to months

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#  TEST OVERLAPPINGS IN TIMES OF COUNTING PROCESS #_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

error_in_time_pre_followup<-
Base_fiscalia_v15f_grant_23_24_long2 %>% 
    dplyr::filter(dplyr::case_when(time <=lag_time & !is.na(lag_time)~T,T~F)) 
# A tibble: 3,031 x 36

Base_fiscalia_v15f_grant_23_24_long2 %>% 
    dplyr::filter(hash_key %in% unique(error_in_time_pre_followup$hash_key)) %>% 
    dplyr::select(hash_key, time, lag_time, motivodeegreso_mod_imp, dias_treat_imp_sin_na, enter_time, fech_ing_num, fech_egres_num, cens_time, treatment)
# A tibble: 0 x 10
# ... with 10 variables: hash_key <chr>, time <dbl>, lag_time <dbl>,
#   motivodeegreso_mod_imp <chr>, dias_treat_imp_sin_na <dbl>,
#   enter_time <dbl>, fech_ing_num <dbl>, fech_egres_num <dbl>,
#   cens_time <dbl>, treatment <int>
Code
# A tibble: 7,706 x 8
# 0039f0d864e402fc7c9b8d326b6a
# 354 808 1296
Code
tbone_desc_merge_total_grant_23_24_post<-
CreateTableOne(vars=c(setdiff(vars_cov, "policonsumo"), "offense_after_adm"), data=  Base_fiscalia_v15f_grant_23_24[,c("hash_key", "motivodeegreso_mod_imp_rec", "edad_al_ing_1", "sex", "edad_ini_cons", 
"escolaridad_rec", "sus_principal_mod", "freq_cons_sus_prin_ord", 
"condicion_ocupacional_corr24", "policonsumo", "num_hijos_mod_joel_bin", 
"tenencia_de_la_vivienda_mod", "macrozona", "dg_cie_10_rec", "dg_trs_cons_sus_or", 
"clas_r", "porc_pobr", "sus_ini_mod_mvv", "ano_nac_corr", "con_quien_vive_joel", 
"fis_comorbidity_icd_10", "compromiso_biopsicosocial", "origen_ingreso_mod","tipo_centro_pub")] %>% dplyr::filter(hash_key %in% unlist(unique(Base_fiscalia_v15f_grant_23_24_long2$hash_key))), factorVars = setdiff(vars_cov, c("motivodeegreso_mod_imp_rec","edad_al_ing_1", "edad_ini_cons","ano_nac_corr", "porc_pobr")), smd=T, strata="policonsumo", addOverall = T, includeNA=T, test=T)

Warning in ModuleReturnVarsExist(vars, data): The data frame does not have: tipo_de_plan_2 tipo_de_plan_2_mod offense_after_adm Dropped

Warning in ModuleReturnVarsExist(factorVars, data): The data frame does not have: tipo_de_plan_2 tipo_de_plan_2_mod Dropped

Code
as.data.frame.TableOne(tbone_desc_merge_total_grant_23_24_post, smd=T, nonnormal= T)%>% 
  dplyr::mutate(char2=characteristic) %>% 
  tidyr::fill(char2) %>% 
  dplyr::select(char2,everything()) %>% 
  dplyr::mutate(level=ifelse(is.na(level),"[Missing]",level)) %>% 
  dplyr::mutate(char2=dplyr::case_when(characteristic=="NA"~NA_character_,TRUE~as.character(characteristic))) %>% 
  format_cells(1, 1:length(names(.)), "bold") %>%
  dplyr::select(-1) %>% 
  dplyr::mutate(across(.fns = ~ str_replace_all(., pattern = "\\( ", replacement = "\\("))) %>% 
  dplyr::mutate(across(.fns = ~ str_trim(.))) %>% 
  knitr::kable(size=10, format="markdown",caption= "Summary descriptives, Polysubstance(1) and no Polysubstance use (0) [exclude ongoing treatments and external referrals]", escape=T)

fill: changed 56 values (71%) of ‘characteristic’ (56 fewer NA)

Summary descriptives, Polysubstance(1) and no Polysubstance use (0) [exclude ongoing treatments and external referrals]
characteristic level Overall 0 1 p test SMD
n 72404 18707 53697
Complete status of treatment (binary) (%) Treatment completion 19278 (26.6) 6404 (34.2) 12874 (24.0) <0.001 0.228
Complete status of treatment (binary) (%) Treatment non-completion 53117 (73.4) 12303 (65.8) 40814 (76.0)
Complete status of treatment (binary) (%) [Missing] 9 (0.0) 0 (0.0) 9 (0.0)
Age (admission to treatment) (median [IQR]) 34.04 [27.38, 42.84] 41.40 [32.14, 50.94] 32.27 [26.47, 39.76] <0.001 nonnorm 0.736
Sex (%) Men 55062 (76.0) 13914 (74.4) 41148 (76.6) <0.001 0.052
Sex (%) Women 17342 (24.0) 4793 (25.6) 12549 (23.4)
Age of Onset of Substance Use (median [IQR]) 15.00 [14.00, 18.00] 17.00 [14.00, 20.00] 15.00 [13.00, 17.00] <0.001 nonnorm 0.419
Educational Attainment (%) 1-More than high school 11489 (15.9) 2495 (13.3) 8994 (16.7) <0.001 0.236
Educational Attainment (%) 2-Completed high school or less 39904 (55.1) 9289 (49.7) 30615 (57.0)
Educational Attainment (%) 3-Completed primary school or less 20685 (28.6) 6797 (36.3) 13888 (25.9)
Educational Attainment (%) [Missing] 326 (0.5) 126 (0.7) 200 (0.4)
Primary Substance (admission to treatment) (%) Alcohol 24244 (33.5) 11695 (62.5) 12549 (23.4) <0.001 0.877
Primary Substance (admission to treatment) (%) Cocaine hydrochloride 13505 (18.7) 1906 (10.2) 11599 (21.6)
Primary Substance (admission to treatment) (%) Cocaine paste 28563 (39.4) 3922 (21.0) 24641 (45.9)
Primary Substance (admission to treatment) (%) Marijuana 4835 (6.7) 784 (4.2) 4051 (7.5)
Primary Substance (admission to treatment) (%) Other 1256 (1.7) 399 (2.1) 857 (1.6)
Primary Substance (admission to treatment) (%) [Missing] 1 (0.0) 1 (0.0) 0 (0.0)
Frequency of Substance Use (Primary Substance) (%) 1. Less than 1 day a week 3554 (4.9) 1276 (6.8) 2278 (4.2) <0.001 0.189
Frequency of Substance Use (Primary Substance) (%) 2. 1 day a week or more 4852 (6.7) 1621 (8.7) 3231 (6.0)
Frequency of Substance Use (Primary Substance) (%) 3. 2 to 3 days a week 20417 (28.2) 5578 (29.8) 14839 (27.6)
Frequency of Substance Use (Primary Substance) (%) 4. 4 to 6 days a week 11855 (16.4) 2969 (15.9) 8886 (16.5)
Frequency of Substance Use (Primary Substance) (%) 5. Daily 31363 (43.3) 7145 (38.2) 24218 (45.1)
Frequency of Substance Use (Primary Substance) (%) [Missing] 363 (0.5) 118 (0.6) 245 (0.5)
Corrected Occupational Status (%) Employed 35924 (49.6) 10709 (57.2) 25215 (47.0) <0.001 0.275
Corrected Occupational Status (%) Inactive 11760 (16.2) 3350 (17.9) 8410 (15.7)
Corrected Occupational Status (%) Unemployed 24719 (34.1) 4647 (24.8) 20072 (37.4)
Corrected Occupational Status (%) [Missing] 1 (0.0) 1 (0.0) 0 (0.0)
Number of Children (dichotomized) (%) 0 16802 (23.2) 3356 (17.9) 13446 (25.0) <0.001 0.174
Number of Children (dichotomized) (%) 1 54984 (75.9) 15188 (81.2) 39796 (74.1)
Number of Children (dichotomized) (%) [Missing] 618 (0.9) 163 (0.9) 455 (0.8)
tenencia_de_la_vivienda_mod (%) Illegal Settlement 763 (1.1) 167 (0.9) 596 (1.1) <0.001 0.269
tenencia_de_la_vivienda_mod (%) Others 2041 (2.8) 522 (2.8) 1519 (2.8)
tenencia_de_la_vivienda_mod (%) Owner/Transferred dwellings/Pays Dividends 25339 (35.0) 8171 (43.7) 17168 (32.0)
tenencia_de_la_vivienda_mod (%) Renting 12354 (17.1) 3145 (16.8) 9209 (17.1)
tenencia_de_la_vivienda_mod (%) Stays temporarily with a relative 27759 (38.3) 5637 (30.1) 22122 (41.2)
tenencia_de_la_vivienda_mod (%) [Missing] 4148 (5.7) 1065 (5.7) 3083 (5.7)
Macro Administrative Zone in Chile (%) Center 54864 (75.8) 13988 (74.8) 40876 (76.1) <0.001 0.292
Macro Administrative Zone in Chile (%) North 10690 (14.8) 1882 (10.1) 8808 (16.4)
Macro Administrative Zone in Chile (%) South 6833 (9.4) 2835 (15.2) 3998 (7.4)
Macro Administrative Zone in Chile (%) [Missing] 17 (0.0) 2 (0.0) 15 (0.0)
Psychiatric Comorbidity (ICD-10) (%) Without psychiatric comorbidity 28308 (39.1) 8542 (45.7) 19766 (36.8) <0.001 0.187
Psychiatric Comorbidity (ICD-10) (%) Diagnosis unknown (under study) 13616 (18.8) 2918 (15.6) 10698 (19.9)
Psychiatric Comorbidity (ICD-10) (%) With psychiatric comorbidity 30480 (42.1) 7247 (38.7) 23233 (43.3)
SUD Severity (Dependence status) (%) Drug dependence 52416 (72.4) 11498 (61.5) 40918 (76.2) <0.001 0.322
SUD Severity (Dependence status) (%) Hazardous consumption 19987 (27.6) 7208 (38.5) 12779 (23.8)
SUD Severity (Dependence status) (%) [Missing] 1 (0.0) 1 (0.0) 0 (0.0)
clas_r (%) Mixed 6938 (9.6) 2359 (12.6) 4579 (8.5) <0.001 0.267
clas_r (%) Rural 5861 (8.1) 2382 (12.7) 3479 (6.5)
clas_r (%) Urban 59603 (82.3) 13965 (74.7) 45638 (85.0)
clas_r (%) [Missing] 2 (0.0) 1 (0.0) 1 (0.0)
Percentage of people in poverty (median [IQR]) 0.12 [0.07, 0.17] 0.11 [0.07, 0.16] 0.12 [0.08, 0.17] <0.001 nonnorm 0.105
Primary Substance (initial diagnosis) (%) Alcohol 39227 (54.2) 12844 (68.7) 26383 (49.1) <0.001 0.506
Primary Substance (initial diagnosis) (%) Cocaine hydrochloride 2681 (3.7) 769 (4.1) 1912 (3.6)
Primary Substance (initial diagnosis) (%) Cocaine paste 3401 (4.7) 1149 (6.1) 2252 (4.2)
Primary Substance (initial diagnosis) (%) Marijuana 19644 (27.1) 2589 (13.8) 17055 (31.8)
Primary Substance (initial diagnosis) (%) Other 1648 (2.3) 457 (2.4) 1191 (2.2)
Primary Substance (initial diagnosis) (%) [Missing] 5803 (8.0) 899 (4.8) 4904 (9.1)
Corrected birth year (median [IQR]) 1980.00 [1971.00, 1987.00] 1973.00 [1964.00, 1983.00] 1981.00 [1974.00, 1988.00] <0.001 nonnorm 0.623
Cohabitation status (Recoded) (f) (%) Alone 6816 (9.4) 2276 (12.2) 4540 (8.5) <0.001 0.337
Cohabitation status (Recoded) (f) (%) Family of origin 30074 (41.5) 5648 (30.2) 24426 (45.5)
Cohabitation status (Recoded) (f) (%) Others 6224 (8.6) 1520 (8.1) 4704 (8.8)
Cohabitation status (Recoded) (f) (%) With couple/children 29289 (40.5) 9262 (49.5) 20027 (37.3)
Cohabitation status (Recoded) (f) (%) [Missing] 1 (0.0) 1 (0.0) 0 (0.0)
Physical Comorbidity (ICD-10) (%) Without physical comorbidity 28661 (39.6) 6946 (37.1) 21715 (40.4) <0.001 0.085
Physical Comorbidity (ICD-10) (%) Diagnosis unknown (under study) 39215 (54.2) 10372 (55.4) 28843 (53.7)
Physical Comorbidity (ICD-10) (%) One or more 4528 (6.3) 1389 (7.4) 3139 (5.8)
Biopsychosocial compromise (%) 1-Mild 6954 (9.6) 3231 (17.3) 3723 (6.9) <0.001 0.418
Biopsychosocial compromise (%) 2-Moderate 41015 (56.6) 11147 (59.6) 29868 (55.6)
Biopsychosocial compromise (%) 3-Severe 22998 (31.8) 3909 (20.9) 19089 (35.5)
Biopsychosocial compromise (%) [Missing] 1437 (2.0) 420 (2.2) 1017 (1.9)
Treatment Admission Motive (%) Spontaneous 32847 (45.4) 8462 (45.2) 24385 (45.4) <0.001 0.191
Treatment Admission Motive (%) Assisted Referral 6753 (9.3) 1222 (6.5) 5531 (10.3)
Treatment Admission Motive (%) Other 3935 (5.4) 984 (5.3) 2951 (5.5)
Treatment Admission Motive (%) Justice Sector 6813 (9.4) 2380 (12.7) 4433 (8.3)
Treatment Admission Motive (%) Health Sector 22056 (30.5) 5659 (30.3) 16397 (30.5)
tipo_centro_pub (%) FALSE 20677 (28.6) 2806 (15.0) 17871 (33.3) <0.001 0.438
tipo_centro_pub (%) TRUE 51710 (71.4) 15899 (85.0) 35811 (66.7)
tipo_centro_pub (%) [Missing] 17 (0.0) 2 (0.0) 15 (0.0)
Code
#kable(size=10, format="html",caption= "Summary descriptives, by Baseline Treatment Status") %>%     kableExtra::kable_classic()

as.data.frame.TableOne(tbone_desc_merge_total_grant_23_24_post, smd=T, nonnormal= T)%>% 
  dplyr::mutate(char2=characteristic) %>% 
  tidyr::fill(char2) %>% 
  dplyr::select(char2,everything()) %>% 
  dplyr::mutate(level=ifelse(is.na(level),"[Missing]",level)) %>% 
  dplyr::mutate(char2=dplyr::case_when(characteristic=="NA"~NA_character_,TRUE~as.character(characteristic))) %>% 
  format_cells(1, 1:length(names(.)), "bold") %>%
  dplyr::select(-1) %>%
  dplyr::mutate(across(.fns = ~ str_replace_all(., pattern = "\\( ", replacement = "\\("))) %>%
  dplyr::mutate(across(.fns = ~ str_trim(.))) %>% 
  write.table(paste0(getwd(),"/_proposal_grant/2023/baseline_psu_desc_smd_post.csv"), dec=",", sep="\t")

fill: changed 56 values (71%) of ‘characteristic’ (56 fewer NA)

Code
#C:\Users\CISS Fondecyt\Mi unidad\Alvacast\SISTRAT 2022 (github)\_proposal_grant/2023
Code
set.seed(21251)
IrregLong::abacus.plot(
    25,
    "time",
    "id",
    Base_fiscalia_v15f_grant_23_24_long2%>% #dplyr::filter(event==1)%>% 
        dplyr::group_by(hash_key) %>%
        dplyr::mutate(is_first_occurrence = if_else(row_number() == 1, 1, 0)) %>%
        dplyr::ungroup() %>%
        dplyr::mutate(id = cumsum(is_first_occurrence)) %>% 
        dplyr::select(-is_first_occurrence) %>% data.frame(),
    0,
    120,
    xlab.abacus = "Time (in months of follow-up)",
    ylab.abacus = "Subject",
    pch.abacus = 16,
    col.abacus = 1
)

Code
#abacus_plot_irrelong
jpeg(filename = "_proposal_grant/2023/_figs/abacus.jpg", units = "in", width=8, height=5, res=500)
set.seed(21251)
IrregLong::abacus.plot(
    25,
    "time",
    "id",
    Base_fiscalia_v15f_grant_23_24_long2%>% #dplyr::filter(event==1)%>% 
        dplyr::group_by(hash_key) %>%
        dplyr::mutate(is_first_occurrence = if_else(row_number() == 1, 1, 0)) %>%
        dplyr::ungroup() %>%
        dplyr::mutate(id = cumsum(is_first_occurrence)) %>% 
        dplyr::select(-is_first_occurrence) %>% data.frame(),
    0,
    120,
    xlab.abacus = "Time (in months of follow-up)",
    ylab.abacus = "Subject",
    pch.abacus = 16,
    col.abacus = 1
)
dev.off()
png 
  2 
Code
summary(subset(Base_fiscalia_v15f_grant_23_24_long2, treatment==1)$max_treatment)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  1.000   1.000   1.000   1.244   1.000  10.000 
Code
#prop. q tienen sólo uno
cat("Only one treatment")
Only one treatment
Code
scales::percent(table(subset(Base_fiscalia_v15f_grant_23_24_long2, treatment==1)$max_treatment)[1] / nrow(subset(Base_fiscalia_v15f_grant_23_24_long2, treatment==1)), accuracy=.1)
      1 
"81.6%" 
Code
#scales::percent(59087  /72404, accuracy=.1 )

cat("Patients with 4 or more treatments")
Patients with 4 or more treatments
Code
scales::percent(
  sum(table(subset(Base_fiscalia_v15f_grant_23_24_long2, treatment==1)$max_treatment)[4:100], na.rm=T)/nrow(subset(Base_fiscalia_v15f_grant_23_24_long2, treatment==1))
)
[1] "1%"
Code
#[1] "1%"


Base_fiscalia_v15f_grant_23_24[,c("hash_key", "motivodeegreso_mod_imp_rec", "edad_al_ing_1", "sex", "edad_ini_cons", 
"escolaridad_rec", "sus_principal_mod", "freq_cons_sus_prin_ord", 
"condicion_ocupacional_corr24", "policonsumo", "num_hijos_mod_joel_bin", 
"tenencia_de_la_vivienda_mod", "macrozona", "dg_cie_10_rec", "dg_trs_cons_sus_or", 
"clas_r", "porc_pobr", "sus_ini_mod_mvv", "ano_nac_corr", "con_quien_vive_joel", 
"fis_comorbidity_icd_10", "compromiso_biopsicosocial", "origen_ingreso_mod","tipo_centro_pub","duplicates_filtered")] %>% dplyr::filter(hash_key %in% unlist(unique(Base_fiscalia_v15f_grant_23_24_long2$hash_key))) %>% dplyr::mutate(duplicates_filtered= dplyr::case_when(as.numeric(duplicates_filtered)>3~4,TRUE~as.numeric(duplicates_filtered))) %>% 
  janitor::tabyl(policonsumo,duplicates_filtered)%>% 
  chisq.test()

Warning in chisq.test(.): Chi-squared approximation may be incorrect


    Pearson's Chi-squared test

data:  .
X-squared = 692.64, df = 4, p-value < 2.2e-16
Code
#X-squared = 693, df = 4, p-value <2e-16

data.frame(
    col1 = c(15527, 39686),
    col2 = c(2475, 9902),
    col3 = c(530, 2841),
    col4 = c(175, 1268)
)%>%  
          mutate(across(everything(), ~ . / sum(.)) * 100) %>%
          mutate(across(everything(), round, digits = 2)) 
   col1 col2  col3  col4
1 28.12   20 15.72 12.13
2 71.88   80 84.28 87.87
Code
#    col1 col2  col3  col4
# 1 28.12   20 15.72 12.13
# 2 71.88   80 84.28 87.87

invisible("Patients with more treatments have more baseline PSU")

cbind.data.frame(motivodeegreso_mod_imp_rec = c("Treatment completion", 
                                                 "Treatment non-completion"), 
               `1` = c(15782, 39428), `2` = c(2622, 9750), 
               `3` = c(654, 2717), `4` = c(220, 1222)) %>%  
          mutate_if(is.numeric,~ . / sum(.) * 100) 

mutate_if: changed 2 values (100%) of ‘1’ (0 new NA)

       changed 2 values (100%) of '2' (0 new NA)
       changed 2 values (100%) of '3' (0 new NA)
       changed 2 values (100%) of '4' (0 new NA)
  motivodeegreso_mod_imp_rec       1        2        3        4
1       Treatment completion 28.5854 21.19302 19.40077 15.25659
2   Treatment non-completion 71.4146 78.80698 80.59923 84.74341
Code
#   motivodeegreso_mod_imp_rec     1     2    3     4
# 1       Treatment completion 28.59 21.19 19.4 15.26
# 2   Treatment non-completion 71.41 78.81 80.6 84.74  

As seen in the previous plot, a few observations had more than one treatment.

Code
paste0("Patients with more than one value of PSU (changes in patterns): ",
Base_fiscalia_v15f_grant_23_24_long2%>% 
    dplyr::group_by(hash_key) %>%
    dplyr::group_by(id) %>% 
    summarise(n_dis_psu=n_distinct(policonsumo2), n_dis_event=n_distinct(event), n_dis_event=n_distinct(dias_treat_imp_sin_na), max_tr=max(treatment)) %>% 
    dplyr::filter(n_dis_psu>1) %>% nrow()
)   
[1] "Patients with more than one value of PSU (changes in patterns): 3235"
Code
#[1] "Patients with more than one value of PSU (changes in patterns): 3235"

paste0("Patients with more than one value of event: ",
Base_fiscalia_v15f_grant_23_24_long2%>% 
    dplyr::group_by(id) %>% 
    summarise(n_dis_psu=n_distinct(policonsumo2), n_dis_event=n_distinct(event), n_dis_dias_tr=n_distinct(dias_treat_imp_sin_na), max_tr=max(treatment)) %>% 
    dplyr::filter(n_dis_event>1) %>% nrow()
)
[1] "Patients with more than one value of event: 4623"
Code
#[1] "Patients with more than one value of event: 4623"
paste0("Patients with more than one value of days in treatment: ",
Base_fiscalia_v15f_grant_23_24_long2%>% 
    dplyr::group_by(id) %>% 
    summarise(n_dis_psu=n_distinct(policonsumo2), n_dis_event=n_distinct(event), n_dis_dias_tr=n_distinct(dias_treat_imp_sin_na), max_tr=max(treatment)) %>% 
    dplyr::filter(n_dis_dias_tr>1) %>% nrow()
)
[1] "Patients with more than one value of days in treatment: 13289"
Code
#[1] "Patients with more than one value of days in treatment: 13289"
paste0("Patients with more than one value in biopsychosocial compromise: ",
Base_fiscalia_v15f_grant_23_24_long2%>% 
   dplyr::group_by(id) %>% 
    summarise(n_dis_psu=n_distinct(policonsumo2), n_dis_event=n_distinct(event), n_dis_dias_tr=n_distinct(dias_treat_imp_sin_na), n_dis_biopsicomp= n_distinct(compromiso_biopsicosocial.y), max_tr=max(treatment)) %>% 
    dplyr::filter(n_dis_biopsicomp>1) %>% nrow()
)
[1] "Patients with more than one value in biopsychosocial compromise: 6691"
Code
#[1] "Patients with more than one value in biopsychosocial compromise: 6691"
Code
library(easyalluvial)
library(parcats)

#tipo_de_plan2
#treatment, dias_treat_imp_sin_na, policonsumo2, event
# basic ambulatory intensive ambulatory          residential 
table(subset(Base_fiscalia_v15f_grant_23_24_long2, treatment==1)$max_treatment)

    1     2     3     4     5     6     7     8    10 
59087 10121  2363   601   163    52    12     4     1 
Code
cat("================================")
================================
Code
invisible("First 4 treatments, stratified by treatment plan, to see if polysubstance use changes by treatment")

# subset(Base_fiscalia_v15f_grant_23_24_long2, hash_key%in% (dplyr::filter(Base_fiscalia_v15f_grant_23_24_long2, tipo_de_plan_2=="residential" & treatment==1) %>% dplyr::pull("hash_key") %>% unique()) & treatment<5,c("id","treatment","motivodeegreso_mod_imp","policonsumo2"))
p_alluvial<-
alluvial_long(data.frame(subset(Base_fiscalia_v15f_grant_23_24_long2, treatment<5) %>% dplyr::mutate(policonsumo2=if_else(policonsumo2==1,"PSU","no PSU",NA_character_))), 
              key= treatment,
              id= id, 
              value= policonsumo2, 
              fill_by="first_variable",
              NA_label = "censored", 
              complete=F,
              bin=3, 
              stratum_label_size=2,
              stratum_width=1/4,
              bin_labels = c("no PSU", "PSU"),
              col_vector_flow=c("red","gray60"),
              col_vector_value=c("#bf6f6f","#eec1ad","gray60") )+
  theme_void()
#1: In .f(.x[[i]], ...) : bins  (0.4544,0.4549] of policonsumo2 are empty

p_alluvial$data$x_numeric <- as.numeric(as.character(p_alluvial$data$x))  # Convert factor to numeric correctly

library(ggalluvial)

Warning: package ‘ggalluvial’ was built under R version 4.1.3

Code
if( !is.na(match("compute",ls())) ){
    p_alluvial$data %>% 
        # dplyr::group_by(x)%>%
        # dplyr::mutate(Percentage = round(n / sum(n[value==]) * 100,1)) %>% 
        # dplyr::mutate(print=glue::glue("{format(n, big.mark=',')}; {Percentage}%"))%>% 
      dplyr::ungroup() %>% 
        ggplot(
           aes(axis1 = x_numeric, axis2 = x_numeric + 1, y = n)) +#, label = print
      geom_alluvium(aes(fill = fill)) +
      geom_stratum() +
      geom_text(stat = "stratum", size = 3) + # Adjust size as needed
      theme_minimal() +
      ggtitle("Alluvial Plot with Custom Flow Labels") +
      xlab("Stage") +
      ylab("Count")
}

df_alluvial<-
p_alluvial$data_key %>% group_by(alluvial_id) %>% slice(1) %>% 
    dplyr::group_by(`1`) %>% 
    dplyr::mutate(perc1=scales::percent(n/sum(n), accuracy=.1)) %>% 
      dplyr::group_by(`1`,`2`)%>% 
    dplyr::mutate(perc2=scales::percent(n/sum(n), accuracy=.1)) %>% 
  ungroup() %>% 
  data.table::data.table()


plot2<-
ggplot(data =df_alluvial,
       aes(axis1 = `1`, axis2 = `2`, axis3 = `3`, y = n)) +
    geom_alluvium(aes(fill = `1`)) +
    geom_stratum(alpha = .5) +
    geom_text(stat = "stratum", aes(label = paste0(after_stat(stratum),"; ",scales::percent(after_stat(prop), .1))), size=4) +
    # geom_text(stat = "flow", nudge_x = 0.3, 
    #           aes(label = paste0(after_stat(flow)," ",round(after_stat(count)*after_stat(prop), 0)), 
    #               hjust = (after_stat(flow) == "to")), 
    #           size=3) +
    scale_fill_manual(name="PSU",values = c("no PSU" = "darkred", "PSU" = "darkblue", "censored" = "gray60")) +
    xlab("Stage") +
  theme_void()+
    theme(legend.position="bottom")


ggsave(plot2, file="_proposal_grant/2023/_figs/alluvial.png", width=8.5, height=5.5, dpi=500)

Warning in to_lodes_form(data = data, axes = axis_ind, discern = params$discern): Some strata appear at multiple axes.

Warning in to_lodes_form(data = data, axes = axis_ind, discern = params$discern): Some strata appear at multiple axes.

Warning in to_lodes_form(data = data, axes = axis_ind, discern = params$discern): Some strata appear at multiple axes.

Code
ggsave(plot2, file="_proposal_grant/2023/_figs/alluvial.jpg", width=8.5, height=5.5, dpi=500)

Warning in to_lodes_form(data = data, axes = axis_ind, discern = params$discern): Some strata appear at multiple axes.

Warning in to_lodes_form(data = data, axes = axis_ind, discern = params$discern): Some strata appear at multiple axes.

Warning in to_lodes_form(data = data, axes = axis_ind, discern = params$discern): Some strata appear at multiple axes.

Code
newdat <- layer_data(plot2)

Warning in to_lodes_form(data = data, axes = axis_ind, discern = params$discern): Some strata appear at multiple axes.

Warning in to_lodes_form(data = data, axes = axis_ind, discern = params$discern): Some strata appear at multiple axes.

Warning in to_lodes_form(data = data, axes = axis_ind, discern = params$discern): Some strata appear at multiple axes.

Code
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
cat("======================================")
======================================
Code
invisible("Database to summarise trajectories")
# data.frame(subset(Base_fiscalia_v15f_grant_23_24_long2, hash_key%in% (dplyr::filter(Base_fiscalia_v15f_grant_23_24_long2, tipo_de_plan_2=="basic ambulatory" & treatment==1) %>% dplyr::pull("hash_key") %>% unique()) & treatment<5,c("id","treatment","dias_treat_imp_sin_na","motivodeegreso_mod_imp","policonsumo2")) %>% dplyr::mutate(policonsumo2= if_else(policonsumo2==1,"PSU","no PSU",NA_character_))) %>%


df_long_trajectories_pre<-
data.frame(subset(Base_fiscalia_v15f_grant_23_24_long2, hash_key %in% (dplyr::filter(Base_fiscalia_v15f_grant_23_24_long2, treatment==1) %>% dplyr::pull("hash_key") %>% unique()) & treatment<5,c("id","treatment","dias_treat_imp_sin_na","motivodeegreso_mod_imp","policonsumo2")) %>% dplyr::mutate(policonsumo2= if_else(policonsumo2==1,"PSU","no PSU",NA_character_))) %>%
  dplyr::mutate(motivodeegreso_mod_imp= dplyr::case_when(dias_treat_imp_sin_na<90 & grepl("Admin",motivodeegreso_mod_imp)~ "Abandono Temprano", dias_treat_imp_sin_na>=90 & grepl("Admin",motivodeegreso_mod_imp)~ "Abandono Tardio", T~  motivodeegreso_mod_imp)) %>% 
  dplyr::select(-dias_treat_imp_sin_na) %>% 
  dplyr::filter(!motivodeegreso_mod_imp %in% c("Derivación","En curso", "Muerte")) %>% 
  dplyr::filter(!is.na(motivodeegreso_mod_imp)) %>% 
  dplyr::ungroup() 

cat("======================================")
======================================
Code
invisible("Database to summarise trajectories until 4th treatment")
df_long_trajectories7<-
df_long_trajectories_pre %>% 
  dplyr::group_by(id) %>%
  dplyr::summarise(trajectory = paste(policonsumo2, motivodeegreso_mod_imp, sep = " -> ", collapse = " -> "), .groups = 'drop') %>% 
  dplyr::group_by(trajectory) %>% 
  dplyr::summarise(count= n(),.groups="drop") %>% 
  dplyr::mutate(repeats = str_count(trajectory, pattern = "->")) %>% 
  dplyr::filter(repeats==max(repeats))  %>% 
  dplyr::mutate(trajectory2= trajectory) %>% 
  tidyr::separate(trajectory2, sep=" -> ", into=paste0("traj",1:(max(.$repeats)+1))) %>% 
  tidyr::unite("trajectory2",paste0("traj",1:(max(.$repeats))), sep= " -> ", na.rm=T) %>% 
  dplyr::group_by(trajectory2) %>% 
  dplyr::arrange(desc(count))%>%
  dplyr::mutate(Percentage = round(count / sum(count) * 100,1)) %>% 
  dplyr::mutate(print=glue::glue("{format(count, big.mark=',')}; {Percentage}%")) %>% 
  dplyr::ungroup() 

df_long_trajectories6<-
df_long_trajectories_pre %>% 
  dplyr::group_by(id) %>%
  dplyr::summarise(trajectory = paste(policonsumo2, motivodeegreso_mod_imp, sep = " -> ", collapse = " -> "), .groups = 'drop') %>% 
  dplyr::group_by(trajectory) %>% 
  dplyr::summarise(count= n(),.groups="drop") %>% 
  dplyr::mutate(repeats = str_count(trajectory, pattern = "->")) %>% 
  tidyr::separate(trajectory, sep=" -> ", into=paste0("traj",1:(max(.$repeats)+1))) %>% 
  tidyr::unite("trajectory",paste0("traj",1:(max(.$repeats))), sep= " -> ", na.rm=T) %>% 
  dplyr::group_by(trajectory) %>% 
  dplyr::summarise(count= sum(count, na.rm=T),.groups="drop") %>% 
  dplyr::arrange(desc(count))%>% 
  dplyr::mutate(repeats = str_count(trajectory, pattern = "->")) %>% 
  dplyr::filter(repeats==max(repeats)) %>% 
  dplyr::mutate(trajectory2= trajectory) %>% 
  tidyr::separate(trajectory2, sep=" -> ", into=paste0("traj",1:(max(.$repeats)+1))) %>% 
  tidyr::unite("trajectory2",paste0("traj",1:(max(.$repeats))), sep= " -> ", na.rm=T) %>% 
  dplyr::group_by(trajectory2) %>% 
  dplyr::arrange(desc(count))%>%
  dplyr::mutate(Percentage = round(count / sum(count) * 100,1)) %>% 
  dplyr::mutate(print=glue::glue("{format(count, big.mark=',')}; {Percentage}%")) %>% 
  dplyr::ungroup() 

df_long_trajectories5<-
df_long_trajectories_pre %>% 
   dplyr::filter(treatment <4) %>% 
  dplyr::group_by(id) %>%
  dplyr::summarise(trajectory = paste(policonsumo2, motivodeegreso_mod_imp, sep = " -> ", collapse = " -> "), .groups = 'drop') %>% 
  dplyr::group_by(trajectory) %>% 
  dplyr::summarise(count= n(),.groups="drop") %>% 
  dplyr::mutate(repeats = str_count(trajectory, pattern = "->")) %>% 
  dplyr::filter(repeats==max(repeats)) %>% 
  dplyr::mutate(trajectory2= trajectory) %>% 
  tidyr::separate(trajectory2, sep=" -> ", into=paste0("traj",1:(max(.$repeats)+1))) %>% 
  tidyr::unite("trajectory2",paste0("traj",1:(max(.$repeats))), sep= " -> ", na.rm=T) %>% 
  dplyr::group_by(trajectory2) %>% 
  dplyr::arrange(desc(count))%>%
  dplyr::mutate(Percentage = round(count / sum(count) * 100,1)) %>% 
  dplyr::mutate(print=glue::glue("{format(count, big.mark=',')}; {Percentage}%")) %>% 
  dplyr::ungroup() 

df_long_trajectories4<-
df_long_trajectories_pre %>% 
   dplyr::filter(treatment <4) %>% 
  dplyr::group_by(id) %>%
  dplyr::summarise(trajectory = paste(policonsumo2, motivodeegreso_mod_imp, sep = " -> ", collapse = " -> "), .groups = 'drop') %>% 
  dplyr::group_by(trajectory) %>% 
  dplyr::summarise(count= n(),.groups="drop") %>% 
  dplyr::mutate(repeats = str_count(trajectory, pattern = "->")) %>% 
  tidyr::separate(trajectory, sep=" -> ", into=paste0("traj",1:(max(.$repeats)+1))) %>% 
  tidyr::unite("trajectory",paste0("traj",1:(max(.$repeats))), sep= " -> ", na.rm=T) %>% 
  dplyr::group_by(trajectory) %>% 
  dplyr::summarise(count= sum(count, na.rm=T),.groups="drop") %>% 
  dplyr::arrange(desc(count))%>% 
  dplyr::mutate(repeats = str_count(trajectory, pattern = "->")) %>% 
  dplyr::filter(repeats==max(repeats)) %>% 
  dplyr::mutate(trajectory2= trajectory) %>% 
  tidyr::separate(trajectory2, sep=" -> ", into=paste0("traj",1:(max(.$repeats)+1))) %>% 
  tidyr::unite("trajectory2",paste0("traj",1:(max(.$repeats))), sep= " -> ", na.rm=T) %>% 
  dplyr::group_by(trajectory2) %>% 
  dplyr::arrange(desc(count))%>%
  dplyr::mutate(Percentage = round(count / sum(count) * 100,1)) %>% 
  dplyr::mutate(print=glue::glue("{format(count, big.mark=',')}; {Percentage}%")) %>% 
  dplyr::ungroup() 

df_long_trajectories3<-
df_long_trajectories_pre %>% 
   dplyr::filter(treatment <3) %>% 
  dplyr::group_by(id) %>%
  dplyr::summarise(trajectory = paste(policonsumo2, motivodeegreso_mod_imp, sep = " -> ", collapse = " -> "), .groups = 'drop') %>% 
  dplyr::group_by(trajectory) %>% 
  dplyr::summarise(count= n(),.groups="drop") %>% 
  dplyr::mutate(repeats = str_count(trajectory, pattern = "->")) %>% 
  dplyr::filter(repeats==max(repeats)) %>% 
  dplyr::mutate(trajectory2= trajectory) %>% 
  tidyr::separate(trajectory2, sep=" -> ", into=paste0("traj",1:(max(.$repeats)+1))) %>% 
  tidyr::unite("trajectory2",paste0("traj",1:(max(.$repeats))), sep= " -> ", na.rm=T) %>% 
  dplyr::group_by(trajectory2) %>% 
  dplyr::arrange(desc(count))%>%
  dplyr::mutate(Percentage = round(count / sum(count) * 100,1)) %>% 
  dplyr::mutate(print=glue::glue("{format(count, big.mark=',')}; {Percentage}%")) %>% 
  dplyr::ungroup() 

df_long_trajectories2<-
df_long_trajectories_pre %>% 
   dplyr::filter(treatment <3) %>% 
  dplyr::group_by(id) %>%
  dplyr::summarise(trajectory = paste(policonsumo2, motivodeegreso_mod_imp, sep = " -> ", collapse = " -> "), .groups = 'drop') %>% 
  dplyr::group_by(trajectory) %>% 
  dplyr::summarise(count= n(),.groups="drop") %>% 
  dplyr::mutate(repeats = str_count(trajectory, pattern = "->")) %>% 
  tidyr::separate(trajectory, sep=" -> ", into=paste0("traj",1:(max(.$repeats)+1))) %>% 
  tidyr::unite("trajectory",paste0("traj",1:(max(.$repeats))), sep= " -> ", na.rm=T) %>% 
  dplyr::group_by(trajectory) %>% 
  dplyr::summarise(count= sum(count, na.rm=T),.groups="drop") %>% 
  dplyr::arrange(desc(count))%>% 
  dplyr::mutate(repeats = str_count(trajectory, pattern = "->")) %>% 
  dplyr::filter(repeats==max(repeats)) %>% 
  dplyr::mutate(trajectory2= trajectory) %>% 
  tidyr::separate(trajectory2, sep=" -> ", into=paste0("traj",1:(max(.$repeats)+1))) %>% 
  tidyr::unite("trajectory2",paste0("traj",1:(max(.$repeats))), sep= " -> ", na.rm=T) %>% 
  dplyr::group_by(trajectory2) %>% 
  dplyr::arrange(desc(count))%>%
  dplyr::mutate(Percentage = round(count / sum(count) * 100,1)) %>% 
  dplyr::mutate(print=glue::glue("{format(count, big.mark=',')}; {Percentage}%")) %>% 
  dplyr::ungroup() 

df_long_trajectories1<-
df_long_trajectories_pre %>% 
   dplyr::filter(treatment <2) %>% 
  dplyr::group_by(id) %>%
  dplyr::summarise(trajectory = paste(policonsumo2, motivodeegreso_mod_imp, sep = " -> ", collapse = " -> "), .groups = 'drop') %>% 
  dplyr::group_by(trajectory) %>% 
  dplyr::summarise(count= n(),.groups="drop") %>% 
  dplyr::mutate(repeats = str_count(trajectory, pattern = "->")) %>% 
  dplyr::filter(repeats==max(repeats)) %>% 
  dplyr::mutate(trajectory2= trajectory) %>% 
  tidyr::separate(trajectory2, sep=" -> ", into=paste0("traj",1:(max(.$repeats)+1))) %>% 
  tidyr::unite("trajectory2",paste0("traj",1:(max(.$repeats))), sep= " -> ", na.rm=T) %>% 
  dplyr::group_by(trajectory2) %>% 
  dplyr::arrange(desc(count))%>%
  dplyr::mutate(Percentage = round(count / sum(count) * 100,1)) %>% 
  dplyr::mutate(print=glue::glue("{format(count, big.mark=',')}; {Percentage}%")) %>% 
  dplyr::ungroup() 

df_long_trajectories0<-
df_long_trajectories_pre %>% 
   dplyr::filter(treatment <2) %>% 
  dplyr::group_by(id) %>%
  dplyr::summarise(trajectory = paste(policonsumo2, motivodeegreso_mod_imp, sep = " -> ", collapse = " -> "), .groups = 'drop') %>% 
  dplyr::group_by(trajectory) %>% 
  dplyr::summarise(count= n(),.groups="drop") %>% 
  dplyr::mutate(repeats = str_count(trajectory, pattern = "->")) %>% 
  tidyr::separate(trajectory, sep=" -> ", into=paste0("traj",1:(max(.$repeats)+1))) %>% 
  tidyr::unite("trajectory",paste0("traj",1:(max(.$repeats))), sep= " -> ", na.rm=T) %>% 
  dplyr::group_by(trajectory) %>% 
  dplyr::summarise(count= sum(count, na.rm=T),.groups="drop") %>% 
  dplyr::arrange(desc(count))%>% 
  dplyr::mutate(repeats = str_count(trajectory, pattern = "->")) %>% 
  dplyr::filter(repeats==max(repeats)) %>% 
  dplyr::arrange(desc(count)) %>%
  dplyr::mutate(Percentage = round(count / sum(count) * 100,1)) %>% 
  dplyr::mutate(print=glue::glue("{format(count, big.mark=',')}; {Percentage}%")) %>% 
  dplyr::ungroup() 

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

cbind.fill <- function(...) {
    transpoted <- lapply(list(...),t)
    transpoted_dataframe <- lapply(transpoted, as.data.table, keep.rownames=T)
    return (data.frame(t(plyr::rbind.fill(transpoted_dataframe))))                                               
} 
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

tree_str<-
    plyr::rbind.fill(cbind(step=1,df_long_trajectories0),
                     cbind(step=2,df_long_trajectories1),
                     cbind(step=3,df_long_trajectories2),
                     cbind(step=4,df_long_trajectories3),
                     cbind(step=5,df_long_trajectories4),
                     cbind(step=6,df_long_trajectories5),
                     cbind(step=7,df_long_trajectories6),
                     cbind(step=8,df_long_trajectories7)) %>% 
  dplyr::mutate(trajectory2 = str_replace_all(trajectory, c(
    "no PSU" = "0", #must be first because it is more specific
    "PSU" = "1",
    "Abandono Temprano" = "0",
    "Abandono Tardio" = "1",
    "Alta Terapéutica" = "2",
    " -> " = "",
    " " = ""
  )))

source(paste0(getwd(),"/_proposal_grant/2023/tree_str_pab.R"))
#tree_str_pab
source(paste0(getwd(),"/_proposal_grant/2023/tree_str_pai.R"))
#tree_str_pai
source(paste0(getwd(),"/_proposal_grant/2023/tree_str_pr.R"))
#tree_str_pr
Code
library(data.tree)

traveller <- Node$new("Admission")
route0 <- traveller$AddChild(paste0("1st tr., no PSU:\n", as.character(subset(tree_str, trajectory2=="0", "print"))
                                    ))
route1 <- traveller$AddChild(paste0("1st tr., PSU:\n", as.character(subset(tree_str, trajectory2=="1", "print"))
                                    ))
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#step 1
route00 <- route0$AddChild(paste0("1st  tr., no PSU, Early drop:\n", 
as.character(subset(tree_str, trajectory2=="00", "print"))
                                    ))
route01 <- route0$AddChild(paste0("1st  tr., no PSU, Late drop:\n", 
as.character(subset(tree_str, trajectory2=="01", "print"))
                                    ))
route02 <- route0$AddChild(paste0("1st  tr., no PSU, Tr. comp:\n", 
as.character(subset(tree_str, trajectory2=="02", "print"))
                                    ))
route10 <- route1$AddChild(paste0("1st  tr., PSU, Early drop:\n", 
as.character(subset(tree_str, trajectory2=="10", "print"))
                                    ))
route11 <- route1$AddChild(paste0("1st  tr., PSU, Late drop:\n", 
as.character(subset(tree_str, trajectory2=="11", "print"))
                                    ))
route12 <- route1$AddChild(paste0("1st  tr., PSU, Tr. comp:\n", 
as.character(subset(tree_str, trajectory2=="12", "print"))
                                    ))
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#step 2
route000 <- route00$AddChild(paste0("2nd tr., no PSU:\n", as.character(subset(tree_str, trajectory2=="000", "print"))
                                    ))
route001 <- route00$AddChild(paste0("2nd tr., PSU:\n", as.character(subset(tree_str, trajectory2=="001", "print"))
                                    ))
route010 <- route01$AddChild(paste0("2nd tr., no PSU:\n", as.character(subset(tree_str, trajectory2=="010", "print"))
                                    ))
route011 <- route01$AddChild(paste0("2nd tr., PSU:\n", as.character(subset(tree_str, trajectory2=="011", "print"))
                                    ))
route020 <- route02$AddChild(paste0("2nd tr., no PSU:\n", as.character(subset(tree_str, trajectory2=="020", "print"))
                                    ))
route021 <- route02$AddChild(paste0("2nd tr., PSU:\n", as.character(subset(tree_str, trajectory2=="021", "print"))
                                    ))
route100 <- route10$AddChild(paste0("2nd tr., no PSU:\n", as.character(subset(tree_str, trajectory2=="100", "print"))
                                    ))
route101 <- route10$AddChild(paste0("2nd tr., PSU:\n", as.character(subset(tree_str, trajectory2=="101", "print"))
                                    ))
route110 <- route11$AddChild(paste0("2nd tr., no PSU:\n", as.character(subset(tree_str, trajectory2=="110", "print"))
                                    ))
route111 <- route11$AddChild(paste0("2nd tr., PSU:\n", as.character(subset(tree_str, trajectory2=="111", "print"))
                                    ))
route120 <- route12$AddChild(paste0("2nd tr., no PSU:\n", as.character(subset(tree_str, trajectory2=="120", "print"))
                                    ))
route121 <- route12$AddChild(paste0("2nd tr., PSU:\n", as.character(subset(tree_str, trajectory2=="121", "print"))
                                    ))
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#step 3
route0000 <- route000$AddChild(paste0("3rd tr., Early drop:\n", as.character(subset(tree_str, trajectory2=="0000", "print"))
                                    ))
route0010 <- route001$AddChild(paste0("3rd tr., Early drop:\n", as.character(subset(tree_str, trajectory2=="0010", "print"))
                                    ))
route0100 <- route010$AddChild(paste0("3rd tr., Early drop:\n", as.character(subset(tree_str, trajectory2=="0100", "print"))
                                    ))
route0110 <- route011$AddChild(paste0("3rd tr., Early drop:\n", as.character(subset(tree_str, trajectory2=="0110", "print"))
                                    ))
route0200 <- route020$AddChild(paste0("3rd tr., Early drop:\n", as.character(subset(tree_str, trajectory2=="0200", "print"))
                                    ))
route0210 <- route021$AddChild(paste0("3rd tr., Early drop:\n", as.character(subset(tree_str, trajectory2=="0210", "print"))
                                    ))
route1000 <- route100$AddChild(paste0("3rd tr., Early drop:\n", as.character(subset(tree_str, trajectory2=="1000", "print"))
                                    ))
route1010 <- route101$AddChild(paste0("3rd tr., Early drop:\n", as.character(subset(tree_str, trajectory2=="1010", "print"))
                                    ))
route1100 <- route110$AddChild(paste0("3rd tr., Early drop:\n", as.character(subset(tree_str, trajectory2=="1100", "print"))
                                    ))
route1110 <- route111$AddChild(paste0("3rd tr., Early drop:\n", as.character(subset(tree_str, trajectory2=="1110", "print"))
                                    ))
route1200 <- route120$AddChild(paste0("3rd tr., Early drop:\n", as.character(subset(tree_str, trajectory2=="1200", "print"))
                                    ))
route1210 <- route121$AddChild(paste0("3rd tr., Early drop:\n", as.character(subset(tree_str, trajectory2=="1210", "print"))
                                    ))

route0001 <- route000$AddChild(paste0("3rd tr., Late drop:\n", as.character(subset(tree_str, trajectory2=="0001", "print"))
                                    ))
route0011 <- route001$AddChild(paste0("3rd tr., Late drop:\n", as.character(subset(tree_str, trajectory2=="0011", "print"))
                                    ))
route0101 <- route010$AddChild(paste0("3rd tr., Late drop:\n", as.character(subset(tree_str, trajectory2=="0101", "print"))
                                    ))
route0111 <- route011$AddChild(paste0("3rd tr., Late drop:\n", as.character(subset(tree_str, trajectory2=="0111", "print"))
                                    ))
route0201 <- route020$AddChild(paste0("3rd tr., Late drop:\n", as.character(subset(tree_str, trajectory2=="0201", "print"))
                                    ))
route0211 <- route021$AddChild(paste0("3rd tr., Late drop:\n", as.character(subset(tree_str, trajectory2=="0211", "print"))
                                    ))
route1001 <- route100$AddChild(paste0("3rd tr., Late drop:\n", as.character(subset(tree_str, trajectory2=="1001", "print"))
                                    ))
route1011 <- route101$AddChild(paste0("3rd tr., Late drop:\n", as.character(subset(tree_str, trajectory2=="1011", "print"))
                                    ))
route1101 <- route110$AddChild(paste0("3rd tr., Late drop:\n", as.character(subset(tree_str, trajectory2=="1101", "print"))
                                    ))
route1111 <- route111$AddChild(paste0("3rd tr., Late drop:\n", as.character(subset(tree_str, trajectory2=="1111", "print"))
                                    ))
route1201 <- route120$AddChild(paste0("3rd tr., Late drop:\n", as.character(subset(tree_str, trajectory2=="1201", "print"))
                                    ))
route1211 <- route121$AddChild(paste0("3rd tr., Late drop:\n", as.character(subset(tree_str, trajectory2=="1211", "print"))
                                    ))

route0002 <- route000$AddChild(paste0("3rd tr., Tr. comp:\n", as.character(subset(tree_str, trajectory2=="0002", "print"))
                                    ))
route0012 <- route001$AddChild(paste0("3rd tr., Tr. comp:\n", as.character(subset(tree_str, trajectory2=="0012", "print"))
                                    ))
route0102 <- route010$AddChild(paste0("3rd tr., Tr. comp:\n", as.character(subset(tree_str, trajectory2=="0102", "print"))
                                    ))
route0112 <- route011$AddChild(paste0("3rd tr., Tr. comp:\n", as.character(subset(tree_str, trajectory2=="0112", "print"))
                                    ))
route0202 <- route020$AddChild(paste0("3rd tr., Tr. comp:\n", as.character(subset(tree_str, trajectory2=="0202", "print"))
                                    ))
route0212 <- route021$AddChild(paste0("3rd tr., Tr. comp:\n", as.character(subset(tree_str, trajectory2=="0212", "print"))
                                    ))
route1002 <- route100$AddChild(paste0("3rd tr., Tr. comp:\n", as.character(subset(tree_str, trajectory2=="1002", "print"))
                                    ))
route1012 <- route101$AddChild(paste0("3rd tr., Tr. comp:\n", as.character(subset(tree_str, trajectory2=="1012", "print"))
                                    ))
route1102 <- route110$AddChild(paste0("3rd tr., Tr. comp:\n", as.character(subset(tree_str, trajectory2=="1102", "print"))
                                    ))
route1112 <- route111$AddChild(paste0("3rd tr., Tr. comp:\n", as.character(subset(tree_str, trajectory2=="1112", "print"))
                                    ))
route1202 <- route120$AddChild(paste0("3rd tr., Tr. comp:\n", as.character(subset(tree_str, trajectory2=="1202", "print"))
                                    ))
route1212 <- route121$AddChild(paste0("3rd tr., Tr. comp:\n", as.character(subset(tree_str, trajectory2=="1212", "print"))
                                    ))

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
SetGraphStyle(traveller, rankdir = "LR")
plot(traveller)


Missingness

Code
paste0("Percentage of the total that has missing values: ",
scales::percent(Base_fiscalia_v15f_grant_23_24_long2[which(complete.cases(subset(Base_fiscalia_v15f_grant_23_24_long2, select= c(hash_key, tipo_de_plan_2, tipo_de_plan_2_mod, sex,  edad_al_ing_1, edad_ini_cons, escolaridad_rec, sus_principal_mod, freq_cons_sus_prin_ord, condicion_ocupacional_corr24, policonsumo, policonsumo2, num_hijos_mod_joel_bin, tenencia_de_la_vivienda_mod, macrozona, dg_cie_10_rec, dg_trs_cons_sus_or, clas_r, porc_pobr, sus_ini_mod_mvv, ano_nac_corr, con_quien_vive_joel, tipo_centro_pub, origen_ingreso_mod, compromiso_biopsicosocial,compromiso_biopsicosocial.y)))),] %>% nrow()/nrow(Base_fiscalia_v15f_grant_23_24_long2), accuracy = 0.1)
, "; total= ", format(nrow(Base_fiscalia_v15f_grant_23_24_long2),big.mark=","))
[1] "Percentage of the total that has missing values: 84.4%; total= 90,075"
Code
#[1] "Percentage of the total that has missing values: 84.4%; total= 90,075"
# 76029

# length(unique(Base_fiscalia_v15f_grant_23_24_long2$hash_key))
#patients: 72404

Base_fiscalia_v15f_grant_23_24_long %>% dplyr::filter(!motivodeegreso_mod_imp %in% c("En curso", "Muerte", "Derivación")) %>%  distinct(hash_key) %>% nrow()
[1] 72404
Code
#[1] 72404

missing_pct <- colMeans(is.na(subset(Base_fiscalia_v15f_grant_23_24_long2, select= c(hash_key, tipo_de_plan_2, tipo_de_plan_2_mod, sex,  edad_al_ing_1, edad_ini_cons, escolaridad_rec, sus_principal_mod, freq_cons_sus_prin_ord, condicion_ocupacional_corr24, policonsumo, policonsumo2, num_hijos_mod_joel_bin, tenencia_de_la_vivienda_mod, macrozona, dg_cie_10_rec, dg_trs_cons_sus_or, clas_r, porc_pobr, sus_ini_mod_mvv, ano_nac_corr, con_quien_vive_joel, tipo_centro_pub, origen_ingreso_mod, compromiso_biopsicosocial, compromiso_biopsicosocial.y
              ) ))) * 100
round(missing_pct,2)
                    hash_key               tipo_de_plan_2 
                        0.00                         0.10 
          tipo_de_plan_2_mod                          sex 
                        0.12                         0.00 
               edad_al_ing_1                edad_ini_cons 
                        0.01                         6.84 
             escolaridad_rec            sus_principal_mod 
                        0.43                         0.00 
      freq_cons_sus_prin_ord condicion_ocupacional_corr24 
                        0.50                         0.00 
                 policonsumo                 policonsumo2 
                        0.00                         0.00 
      num_hijos_mod_joel_bin  tenencia_de_la_vivienda_mod 
                        0.81                         5.84 
                   macrozona                dg_cie_10_rec 
                        0.02                         0.00 
          dg_trs_cons_sus_or                       clas_r 
                        0.00                         0.00 
                   porc_pobr              sus_ini_mod_mvv 
                        0.00                         6.64 
                ano_nac_corr          con_quien_vive_joel 
                        0.01                         0.00 
             tipo_centro_pub           origen_ingreso_mod 
                        0.02                         0.00 
   compromiso_biopsicosocial  compromiso_biopsicosocial.y 
                        1.95                         1.94 
Code
set.seed(2125)
Base_fiscalia_v15f_grant_23_24_long2_miss <- Base_fiscalia_v15f_grant_23_24_long2 %>% 
  dplyr::arrange(hash_key, fech_ing_num) %>% 
   missRanger::missRanger(
                formula =  sus_principal_mod + condicion_ocupacional_corr24 + dg_trs_cons_sus_or + con_quien_vive_joel + clas_r + porc_pobr + edad_al_ing_1 + ano_nac_corr +  macrozona + tipo_centro_pub + tipo_de_plan_2 + tipo_de_plan_2_mod + escolaridad_rec + freq_cons_sus_prin_ord + num_hijos_mod_joel_bin + compromiso_biopsicosocial.y + compromiso_biopsicosocial + tenencia_de_la_vivienda_mod + sus_ini_mod_mvv + edad_ini_cons ~ edad_al_ing_1 + sex + edad_ini_cons + escolaridad_rec + sus_principal_mod + freq_cons_sus_prin_ord + condicion_ocupacional_corr24 + policonsumo + num_hijos_mod_joel_bin + tenencia_de_la_vivienda_mod + macrozona + dg_cie_10_rec + dg_trs_cons_sus_or + clas_r + porc_pobr + sus_ini_mod_mvv + ano_nac_corr + con_quien_vive_joel + tipo_centro_pub + origen_ingreso_mod + compromiso_biopsicosocial + cens_time + policonsumo2 + compromiso_biopsicosocial.y + tipo_de_plan_2 + tipo_de_plan_2_mod, 
                num.trees = 300, 
                pmm.k = 5,                
                returnOOB=T,
                maxiter= 50,
                verbose = 2, 
                seed = 2125)

Missing value imputation by random forests

  Variables to impute:      sus_principal_mod, condicion_ocupacional_corr24, dg_trs_cons_sus_or, con_quien_vive_joel, clas_r, porc_pobr, edad_al_ing_1, ano_nac_corr, macrozona, tipo_centro_pub, tipo_de_plan_2, tipo_de_plan_2_mod, escolaridad_rec, freq_cons_sus_prin_ord, num_hijos_mod_joel_bin, compromiso_biopsicosocial.y, compromiso_biopsicosocial, tenencia_de_la_vivienda_mod, sus_ini_mod_mvv, edad_ini_cons
  Variables used to impute: edad_al_ing_1, sex, edad_ini_cons, escolaridad_rec, sus_principal_mod, freq_cons_sus_prin_ord, condicion_ocupacional_corr24, policonsumo, num_hijos_mod_joel_bin, tenencia_de_la_vivienda_mod, macrozona, dg_cie_10_rec, dg_trs_cons_sus_or, clas_r, porc_pobr, sus_ini_mod_mvv, ano_nac_corr, con_quien_vive_joel, tipo_centro_pub, origen_ingreso_mod, compromiso_biopsicosocial, cens_time, policonsumo2, compromiso_biopsicosocial.y, tipo_de_plan_2, tipo_de_plan_2_mod
    ss_pr_  cn__24  dg____  cn_q__  clas_r  prc_pb  ed___1  an_nc_  macrzn  tp_cn_  tp___2  t___2_  esclr_  fr____  nm____  cmpr_.  cmprm_  tn____  ss_n__  edd_n_
iter 1: 0.4744  0.4529  0.2608  0.4665  0.1676  0.5058  0.6236  0.0106  0.1195  0.1534  0.3502  0.0965  0.3121  0.3646  0.6032  0.3099  0.0592  0.3085  0.2574  0.5780  
iter 2: 1e-04   0.0000  0.0000  0.0000  0.0000  0.0084  0.0013  0.0011  0.0000  0.0000  0.0000  0.0000  0.0000  1e-04   0.0012  0.0000  0.0000  4e-04   1e-04   0.0186  
iter 3: 1e-04   0.0000  0.0000  0.0000  0.0000  0.0083  0.0012  0.0012  0.0000  0.0000  0.0000  0.0000  0.0000  1e-04   0.0011  0.0000  0.0000  5e-04   1e-04   0.0188  
Code
invisible("2024-04-25: ! Can't subset columns that don't exist; Column `vars_cov` doesn't exist. I corrected that by including explicitly the covariates")
library(arrow)
library(rio)
arrow::write_parquet(Base_fiscalia_v15f_grant_23_24_long2_miss, 
  "_proposal_grant/2023/Base_fiscalia_v15f_grant_23_24_long2_miss240425.gz.parquet", 
  compression = "gzip", compression_level = 5)

column_names <- c(
  "sus_principal_mod", "condicion_ocupacional_corr24", "dg_trs_cons_sus_or", "con_quien_vive_joel",
  "clas_r", "porc_pobr", "edad_al_ing_1", "ano_nac_corr", "macrozona", "tipo_centro_pub",
  "tipo_de_plan_2", "tipo_de_plan_2_mod", "escolaridad_rec", "freq_cons_sus_prin_ord",
  "num_hijos_mod_joel_bin", "compromiso_biopsicosocial.y", "compromiso_biopsicosocial",
  "tenencia_de_la_vivienda_mod", "sus_ini_mod_mvv", "edad_ini_cons"
)
rmses_reduction <- matrix(
  c(
    0.4744, 0.4529, 0.2608, 0.4665, 0.1676, 0.5058, 0.6236, 0.0106, 0.1195, 0.1534, 0.3502, 0.0965, 0.3121, 0.3646, 0.6032, 0.3099, 0.0592, 0.3085, 0.2574, 0.5780, 1e-04, 0.0000, 0.0000, 0.0000, 0.0000, 0.0084, 0.0013, 0.0011, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1e-04, 0.0012, 0.0000, 0.0000, 4e-04, 1e-04, 0.0186, 1e-04, 0.0000, 0.0000, 0.0000, 0.0000, 0.0083, 0.0012, 0.0012, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1e-04, 0.0011, 0.0000, 0.0000, 5e-04, 1e-04, 0.0188
  ),
  nrow = 3, byrow = TRUE
)
rmses_reduction <- data.frame(rmses_reduction)
colnames(rmses_reduction) <- column_names



#dput(round(attr(Base_fiscalia_v15f_grant_23_24_long2_miss,"oob"),3))
print("Out of bag error:")
[1] "Out of bag error:"
Code
c(sus_principal_mod = 0, 
  condicion_ocupacional_corr24 = 0, 
  dg_trs_cons_sus_or = 0, 
  con_quien_vive_joel = 0, 
  clas_r = 0, 
  porc_pobr = 0.008, 
  edad_al_ing_1 = 0.001, 
  ano_nac_corr = 0.001, 
  macrozona = 0, 
  tipo_centro_pub = 0, 
  tipo_de_plan_2 = 0, tipo_de_plan_2_mod = 0, 
  escolaridad_rec = 0, 
  freq_cons_sus_prin_ord = 0, 
  num_hijos_mod_joel_bin = 0.001, 
  compromiso_biopsicosocial.y = 0, 
  compromiso_biopsicosocial = 0, 
  tenencia_de_la_vivienda_mod = 0, 
  sus_ini_mod_mvv = 0, 
  edad_ini_cons = 0.019)
           sus_principal_mod condicion_ocupacional_corr24 
                       0.000                        0.000 
          dg_trs_cons_sus_or          con_quien_vive_joel 
                       0.000                        0.000 
                      clas_r                    porc_pobr 
                       0.000                        0.008 
               edad_al_ing_1                 ano_nac_corr 
                       0.001                        0.001 
                   macrozona              tipo_centro_pub 
                       0.000                        0.000 
              tipo_de_plan_2           tipo_de_plan_2_mod 
                       0.000                        0.000 
             escolaridad_rec       freq_cons_sus_prin_ord 
                       0.000                        0.000 
      num_hijos_mod_joel_bin  compromiso_biopsicosocial.y 
                       0.001                        0.000 
   compromiso_biopsicosocial  tenencia_de_la_vivienda_mod 
                       0.000                        0.000 
             sus_ini_mod_mvv                edad_ini_cons 
                       0.000                        0.019 
Code
#OOB prediction error per iteration and variable (1 minus R-squared for regression)

#The default mtry in missRanger is sqrt(p), where p is the number of variables in the dataset.
#OOB prediction errors are quantified as 1 - R^2 for numeric variables, and as classification error otherwise. If a variable has been imputed only univariately, the value is 1.
#https://rdrr.io/cran/missRanger/man/missRanger.html


IrreLong

Irregularity

We first formatted the data according what is being needed by the method used here: id which represents hash_keys in a numeric factored format, and generated the variable less90d_tr which is a variable that groups days in treatment between 0-90 in 1, and 0 for anything else. Additionally, we renamed the variable of biopsychosocial compromise longitudinally measured ( compromiso_biopsicosocial.y) for comp_bpsc_y for ease of processing.

Code
Base_fiscalia_v15f_grant_23_24_long2_proc<-
Base_fiscalia_v15f_grant_23_24_long2%>% #dplyr::filter(event==1)%>% 
  dplyr::group_by(hash_key) %>%
  dplyr::mutate(is_first_occurrence = if_else(row_number() == 1, 1, 0)) %>%
#para hacer ID
  dplyr::ungroup() %>%
  dplyr::mutate(id = cumsum(is_first_occurrence)) %>% 
  dplyr::select(-is_first_occurrence) %>%
  dplyr::mutate(less_90d_tr= factor(if_else(dias_treat_imp_sin_na<=90 & dias_treat_imp_sin_na>0,1,0,NA_real_)))%>%
  dplyr::rename("comp_bpsc_y"="compromiso_biopsicosocial.y") %>% 
  data.frame()

Base_fiscalia_v15f_grant_23_24_long2_miss_proc<-
Base_fiscalia_v15f_grant_23_24_long2_miss%>% #dplyr::filter(event==1)%>% 
  dplyr::group_by(hash_key) %>%
  dplyr::mutate(is_first_occurrence = if_else(row_number() == 1, 1, 0)) %>%
#para hacer ID
  dplyr::ungroup() %>%
  dplyr::mutate(id = cumsum(is_first_occurrence)) %>% 
  dplyr::select(-is_first_occurrence) %>%
  dplyr::mutate(less_90d_tr= factor(if_else(dias_treat_imp_sin_na<=90 & dias_treat_imp_sin_na>0,1,0,NA_real_)))%>%
  dplyr::rename("comp_bpsc_y"="compromiso_biopsicosocial.y") %>% 
  data.frame()

invisible("parece que había que especificar bien las palabras, no permite data.tables")

Next, we measured the extent of irregularity between observation times and gaps between observations. We replicated this analysis in the imputed and original databases, and using calendar time as a reference of time taken.

Code
invisible(" Measuring extent of irregularity")

counts <- extent.of.irregularity(Base_fiscalia_v15f_grant_23_24_long2_proc,
                                 time="time",
                                 id="id",
   scheduledtimes=NULL, cutpoints=NULL,ncutpts=50, 
   maxfu=16*24, plot=F, legendx=30, legendy=0.8,
  formula=Surv(time.lag,time,event)~1,tau=12*12)

counts_counts<-
matrix(c(0, 0.81609, 0.18391, 0.4476, 0.49165, 0.06075, 0.61607, 
0.35612, 0.02781, 0.70566, 0.27893, 0.0154, 0.76138, 0.22912, 
0.0095, 0.7994, 0.19428, 0.00632, 0.82702, 0.16847, 0.00451, 
0.84794, 0.14878, 0.00327, 0.86434, 0.1332, 0.00245, 0.87765, 
0.12038, 0.00197, 0.88848, 0.11, 0.00152, 0.8976, 0.10118, 0.00122, 
0.90537, 0.09359, 0.00103, 0.91202, 0.08712, 0.00085, 0.91781, 
0.08147, 0.00072, 0.92287, 0.07653, 6e-04, 0.92734, 0.07216, 
5e-04, 0.93132, 0.06826, 0.00042, 0.9349, 0.06473, 0.00037, 0.93815, 
0.06151, 0.00034, 0.94107, 0.05863, 3e-04, 0.94371, 0.05603, 
0.00025, 0.94615, 0.05361, 0.00023, 0.94838, 0.05141, 0.00021, 
0.95043, 0.04938, 0.00019, 0.95233, 0.0475, 0.00017, 0.95408, 
0.04576, 0.00015, 0.95572, 0.04413, 0.00015, 0.95724, 0.04262, 
0.00014, 0.95866, 0.04122, 0.00012, 0.95999, 0.03989, 0.00012, 
0.96123, 0.03867, 1e-04, 0.9624, 0.0375, 1e-04, 0.9635, 0.03642, 
8e-05, 0.96454, 0.03538, 8e-05, 0.96552, 0.03441, 7e-05, 0.96645, 
0.03348, 7e-05, 0.96733, 0.03261, 7e-05, 0.96817, 0.03177, 6e-05, 
0.96896, 0.03099, 6e-05, 0.96971, 0.03025, 5e-05, 0.97043, 0.02952, 
5e-05, 0.97112, 0.02883, 5e-05, 0.97177, 0.02818, 4e-05, 0.9724, 
0.02756, 4e-05, 0.973, 0.02696, 4e-05, 0.97357, 0.02639, 4e-05, 
0.97412, 0.02584, 4e-05, 0.97464, 0.02533, 3e-05, 0.97515, 0.02482, 
3e-05), 
byrow=T,
 nrow = 50, ncol= 3, dimnames = list(NULL, c("X1", "X2", "X3")))
counts$auc
[1] 0.06548913
Code
# $auc
# [1] 0.06548913
counts$transformed.auc
[1] 20.25358
Code
# $transformed.auc
# [1] 20.25358

invisible("Is not that much, but is more than 0")


#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

counts_miss <- extent.of.irregularity(Base_fiscalia_v15f_grant_23_24_long2_miss_proc,
                                 time="time",
                                 id="id",
   scheduledtimes=NULL, cutpoints=NULL,ncutpts=50, 
   maxfu=16*24, plot=F, legendx=30, legendy=0.8,
  formula=Surv(time.lag,time,event)~1,tau=12*12)


counts_miss_counts<-
matrix(c(0, 0.81609, 0.18391, 0.4476, 0.49165, 0.06075, 0.61607, 
0.35612, 0.02781, 0.70566, 0.27893, 0.0154, 0.76138, 0.22912, 
0.0095, 0.7994, 0.19428, 0.00632, 0.82702, 0.16847, 0.00451, 
0.84794, 0.14878, 0.00327, 0.86434, 0.1332, 0.00245, 0.87765, 
0.12038, 0.00197, 0.88848, 0.11, 0.00152, 0.8976, 0.10118, 0.00122, 
0.90537, 0.09359, 0.00103, 0.91202, 0.08712, 0.00085, 0.91781, 
0.08147, 0.00072, 0.92287, 0.07653, 6e-04, 0.92734, 0.07216, 
5e-04, 0.93132, 0.06826, 0.00042, 0.9349, 0.06473, 0.00037, 0.93815, 
0.06151, 0.00034, 0.94107, 0.05863, 3e-04, 0.94371, 0.05603, 
0.00025, 0.94615, 0.05361, 0.00023, 0.94838, 0.05141, 0.00021, 
0.95043, 0.04938, 0.00019, 0.95233, 0.0475, 0.00017, 0.95408, 
0.04576, 0.00015, 0.95572, 0.04413, 0.00015, 0.95724, 0.04262, 
0.00014, 0.95866, 0.04122, 0.00012, 0.95999, 0.03989, 0.00012, 
0.96123, 0.03867, 1e-04, 0.9624, 0.0375, 1e-04, 0.9635, 0.03642, 
8e-05, 0.96454, 0.03538, 8e-05, 0.96552, 0.03441, 7e-05, 0.96645, 
0.03348, 7e-05, 0.96733, 0.03261, 7e-05, 0.96817, 0.03177, 6e-05, 
0.96896, 0.03099, 6e-05, 0.96971, 0.03025, 5e-05, 0.97043, 0.02952, 
5e-05, 0.97112, 0.02883, 5e-05, 0.97177, 0.02818, 4e-05, 0.9724, 
0.02756, 4e-05, 0.973, 0.02696, 4e-05, 0.97357, 0.02639, 4e-05, 
0.97412, 0.02584, 4e-05, 0.97464, 0.02533, 3e-05, 0.97515, 0.02482, 
3e-05), 
# 2024-03-07:  i realized that i retrieved the information wrong so i need the byrow
byrow=T,
 nrow = 50, ncol= 3, dimnames = list(NULL, c("X1", "X2", "X3")))

counts_miss$auc
[1] 0.06548913
Code
#[1] 0.06548913

counts_miss$transformed.auc
[1] 20.25358
Code
#[1] 20.25358

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
invisible("2024-04-27: restrict to people w/ >1 treatment")

Base_fiscalia_v15f_grant_23_24_long2_miss_proc_multtr<-
Base_fiscalia_v15f_grant_23_24_long2_miss_proc %>% 
  dplyr::group_by(hash_key) %>% dplyr::mutate(n=n()) %>% dplyr::mutate(is_first_occurrence = if_else(row_number() == 1, 1, 0)) %>% dplyr::ungroup() %>% dplyr::filter(case_when(n>1~ T,T~F)) %>% dplyr::mutate(id = factor(cumsum(is_first_occurrence))) %>% data.frame()
    #dplyr::filter(max_treatment>1)


paste0("p= ", length(unique(Base_fiscalia_v15f_grant_23_24_long2_miss_proc_multtr$hash_key)),
       "; n= ", nrow(Base_fiscalia_v15f_grant_23_24_long2_miss_proc_multtr))
[1] "p= 13317; n= 30988"
Code
counts_restricted <- extent.of.irregularity(Base_fiscalia_v15f_grant_23_24_long2_miss_proc_multtr,
                                 time="time",
                                 id="id",
   scheduledtimes=NULL, cutpoints=NULL,ncutpts=50, 
   maxfu=16*24, plot=F, legendx=30, legendy=0.8,
  formula=Surv(time.lag,time,event)~1,tau=12*12)

#counts_restricted$counts %>% round(.,5) %>% dput()
counts_restricted_counts<-
matrix(c(0, 8e-05, 0.99992, 0.21724, 0.45048, 0.33228, 0.39361, 
0.45328, 0.15311, 0.51048, 0.40435, 0.08517, 0.59123, 0.35607, 
0.0527, 0.64965, 0.31514, 0.03522, 0.69401, 0.28082, 0.02517, 
0.7286, 0.25287, 0.01853, 0.75589, 0.23032, 0.01379, 0.77875, 
0.21026, 0.01099, 0.79751, 0.19379, 0.0087, 0.81332, 0.17973, 
0.00695, 0.8271, 0.16702, 0.00588, 0.83873, 0.1565, 0.00477, 
0.84907, 0.14687, 0.00405, 0.85816, 0.13838, 0.00347, 0.86615, 
0.13091, 0.00294, 0.87323, 0.12434, 0.00243, 0.87973, 0.11814, 
0.00213, 0.88562, 0.11247, 0.00191, 0.89094, 0.10736, 0.0017, 
0.89575, 0.10278, 0.00147, 0.90021, 0.09844, 0.00135, 0.90428, 
0.09453, 0.0012, 0.90803, 0.09088, 0.00109, 0.91152, 0.08749, 
0.00099, 0.91473, 0.08438, 0.00089, 0.91773, 0.08145, 0.00082, 
0.92056, 0.07867, 0.00077, 0.92317, 0.07612, 0.00071, 0.92562, 
0.07371, 0.00067, 0.92791, 0.07148, 0.00061, 0.93006, 0.06938, 
0.00056, 0.93207, 0.06743, 5e-04, 0.93399, 0.06556, 0.00046, 
0.93578, 0.06382, 4e-04, 0.93752, 0.06209, 4e-04, 0.93916, 0.06046, 
0.00038, 0.94071, 0.05892, 0.00037, 0.94216, 0.05751, 0.00033, 
0.94356, 0.05613, 0.00031, 0.94491, 0.05478, 0.00031, 0.94617, 
0.05356, 0.00028, 0.94736, 0.05239, 0.00025, 0.94853, 0.05123, 
0.00024, 0.94965, 0.05012, 0.00023, 0.95072, 0.04906, 0.00022, 
0.95173, 0.04806, 2e-04, 0.95271, 0.04709, 0.00019, 0.95365, 
0.04617, 0.00018), 
# 2024-03-07:  i realized that i retrieved the information wrong so i need the byrow
byrow=T,
 nrow = 50, ncol= 3, dimnames = list(NULL, c("X1", "X2", "X3")))

counts_restricted$auc
[1] 0.212881
Code
#[1] 0.213

counts_restricted$transformed.auc
[1] 80.02791
Code
#80 
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
invisible("Check if there were any errors in of lag times after time")

error_in_time_followup<-
Base_fiscalia_v15f_grant_23_24_long2_proc %>% 
  dplyr::group_by(hash_key) %>% 
  dplyr::mutate(lag_time= lag(time)) %>% 
  dplyr::ungroup() %>% 
  dplyr::filter(dplyr::case_when(time <=lag_time & !is.na(lag_time)~T,T~F)) %>% 
  dplyr::select(hash_key, time, lag_time, motivodeegreso_mod_imp, dias_treat_imp_sin_na, enter_time, fech_ing_num, fech_egres_num, cens_time)
# A tibble: 0 x 9

Base_fiscalia_v15f_grant_23_24_long2_proc %>% 
    dplyr::group_by(hash_key) %>% 
  dplyr::mutate(lag_time= lag(time)) %>% 
  dplyr::ungroup() %>% 
  dplyr::filter(hash_key %in% unique(error_in_time_followup$hash_key)) %>% 
  dplyr::select(hash_key, time, motivodeegreso_mod_imp, dias_treat_imp_sin_na, enter_time, fech_ing_num, fech_egres_num, cens_time)
# A tibble: 0 x 8
# ... with 8 variables: hash_key <chr>, time <dbl>,
#   motivodeegreso_mod_imp <chr>, dias_treat_imp_sin_na <dbl>,
#   enter_time <dbl>, fech_ing_num <dbl>, fech_egres_num <dbl>, cens_time <dbl>
Code
# A tibble: 0 x 8

error_in_time_followup_miss<-
Base_fiscalia_v15f_grant_23_24_long2_miss_proc %>% 
  dplyr::group_by(hash_key) %>% 
  dplyr::mutate(lag_time= lag(time)) %>% 
  dplyr::ungroup() %>% 
  dplyr::filter(dplyr::case_when(time <=lag_time & !is.na(lag_time)~T,T~F)) %>% 
  dplyr::select(hash_key, time, lag_time, motivodeegreso_mod_imp, dias_treat_imp_sin_na, enter_time, fech_ing_num, fech_egres_num, cens_time)
# A tibble: 0 x 9

Base_fiscalia_v15f_grant_23_24_long2_miss_proc %>% 
    dplyr::group_by(hash_key) %>% 
  dplyr::mutate(lag_time= lag(time)) %>% 
  dplyr::ungroup() %>% 
  dplyr::filter(hash_key %in% unique(error_in_time_followup_miss$hash_key)) %>% 
  dplyr::select(hash_key, time, motivodeegreso_mod_imp, dias_treat_imp_sin_na, enter_time, fech_ing_num, fech_egres_num, cens_time) %>%
  {
    if (nrow(.) > 0) {
      stop("There are ties or previous events that occured later than the actual events")
    } else {
      # Behavior when the condition isn't met (e.g., return an empty data frame)
      data.frame(.) 
    }  
  }
[1] hash_key               time                   motivodeegreso_mod_imp
[4] dias_treat_imp_sin_na  enter_time             fech_ing_num          
[7] fech_egres_num         cens_time             
<0 rows> (or 0-length row.names)
Code
# [1] hash_key               time                   motivodeegreso_mod_imp dias_treat_imp_sin_na  enter_time            
# [6] fech_ing_num           fech_egres_num         cens_time             
# <0 rows> (or 0-length row.names)
invisible( "There are no ties or previous events that occured later than the following events")

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

invisible("Now we do it with calendar date unaltered")
set.seed(2125)
counts_corr <- extent.of.irregularity(dplyr::mutate(Base_fiscalia_v15f_grant_23_24_long2_proc, fech_egres_num= fech_egres_num/30.5),
                                 time="fech_egres_num",
                                 id="id",
   scheduledtimes=NULL, cutpoints=NULL,ncutpts=50, 
   maxfu=18213/30.5, plot=F, legendx=30, legendy=0.8,
  formula=Surv(fech_egres_num.lag,fech_egres_num,event)~1,tau=12*12)

counts_corr_counts<-
matrix(c(0.00023, 0.81592, 0.18384, 0.46307, 0.46597, 0.07096, 
0.62881, 0.33313, 0.03807, 0.71395, 0.26336, 0.02269, 0.76724, 
0.21789, 0.01487, 0.80367, 0.18596, 0.01037, 0.8302, 0.16231, 
0.00749, 0.85036, 0.14405, 0.00559, 0.86621, 0.12954, 0.00425, 
0.87903, 0.11765, 0.00332, 0.88967, 0.10767, 0.00266, 0.89855, 
0.09931, 0.00214, 0.90617, 0.09204, 0.00179, 0.91267, 0.08586, 
0.00147, 0.91832, 0.08047, 0.00121, 0.92335, 0.07558, 0.00107, 
0.92776, 0.07134, 9e-04, 0.93165, 0.06762, 0.00073, 0.93519, 
0.06417, 0.00064, 0.93839, 0.06104, 0.00057, 0.94128, 0.05822, 
5e-04, 0.94391, 0.05566, 0.00043, 0.94631, 0.05332, 0.00038, 
0.94852, 0.05115, 0.00034, 0.95054, 0.04917, 0.00029, 0.95245, 
0.04727, 0.00028, 0.95418, 0.04558, 0.00024, 0.95581, 0.04398, 
0.00022, 0.9573, 0.04251, 0.00019, 0.95873, 0.04108, 0.00019, 
0.96004, 0.0398, 0.00016, 0.96129, 0.03855, 0.00016, 0.96245, 
0.03741, 0.00014, 0.96355, 0.03633, 0.00013, 0.96458, 0.0353, 
0.00012, 0.96555, 0.03435, 1e-04, 0.96649, 0.03342, 1e-04, 0.96737, 
0.03254, 9e-05, 0.96819, 0.03172, 8e-05, 0.96898, 0.03094, 8e-05, 
0.96974, 0.03019, 7e-05, 0.97046, 0.02948, 7e-05, 0.97113, 0.02881, 
5e-05, 0.97179, 0.02815, 6e-05, 0.97242, 0.02753, 5e-05, 0.97301, 
0.02694, 5e-05, 0.97358, 0.02638, 4e-05, 0.97413, 0.02583, 4e-05, 
0.97466, 0.0253, 4e-05, 0.97516, 0.0248, 4e-05), 
 nrow = 50, ncol= 3,
# i realized that i needed the byrow=T because i used bad the reading of the dput
byrow=T, dimnames = list(NULL, c("X1", "X2", "X3")))

counts_corr$auc
[1] 0.07266227
Code
# $auc
# [1] 0.07266227
# 
counts_corr$transformed.auc
[1] 22.65514
Code
# $transformed.auc
# [1] 22.65514

if(exists("omitted")){
  invisible("To check specific gap bin widths for the 3 options in a specific time after 'filter'==1 (e.g.)")
rbind.data.frame(cbind.data.frame(type=rep("1. Modified",), time= 1:50, counts_counts),cbind.data.frame(type=rep("2. Modified\n(imputation)",), time= 1:50, counts_miss_counts), cbind.data.frame(type=rep("3. More 1\ntreatment",), time= 1:50, counts_restricted_counts), cbind.data.frame(type=rep("4. Original\ncalendar",), time= 1:50, counts_corr_counts)) %>% dplyr::filter(time==2) %>% dplyr::mutate_if(is.numeric,~round(.,2))

rbind.data.frame(cbind.data.frame(type=rep("1. Modified",), time= 1:50, counts_counts),cbind.data.frame(type=rep("2. Modified\n(imputation)",), time= 1:50, counts_miss_counts), cbind.data.frame(type=rep("3. More 1\ntreatment",), time= 1:50, counts_restricted_counts), cbind.data.frame(type=rep("4. Original\ncalendar",), time= 1:50, counts_corr_counts)) %>% dplyr::filter(time==6) %>% dplyr::mutate_if(is.numeric,~round(.,2))

#2024-05-13
cbind.data.frame(type=rep("5. Restricted\ntreatments",), time= 1:50, counts_restricted_counts) %>% dplyr::filter(time==2) %>% dplyr::mutate_if(is.numeric,~round(.,2))

cbind.data.frame(type=rep("5. Restricted\ntreatments",), time= 1:50, counts_restricted_counts) %>% dplyr::filter(time==6) %>% dplyr::mutate_if(is.numeric,~round(.,2))
}

plot_irregularity<-
rbind.data.frame(cbind.data.frame(type=rep("1. Modified",), time= 1:50, counts_counts),cbind.data.frame(type=rep("2. Modified\n(imputation)",), time= 1:50, counts_miss_counts),cbind.data.frame(type=rep("3. More 1\ntreatment",), time= 1:50, counts_restricted_counts),cbind.data.frame(type=rep("4. Original\ncalendar",), time= 1:50, counts_corr_counts)) %>% 
  reshape2::melt(id=c("type", "time")) %>% 
  dplyr::mutate(Observations= factor(variable, labels=c("0 obs per bin","1 obs per bin", "2+ obs per bin"))) %>% 
  ggplot(aes(x=time, y=value, linetype=Observations), linewidth=1)+
  geom_line()+
  facet_wrap(~type)+ #color=factor(type))
  theme_classic()+
  xlab("Bin width (%)")+
  ylab("Mean proportions of individuals")#+
  # scale_linetype_manual(name="Observations")

plot_irregularity

Code
plot_irregularity_05_2024<-
rbind.data.frame(cbind.data.frame(type=rep("3. More 1\ntreatment",), time= 1:50, counts_restricted_counts)) %>% 
    reshape2::melt(id=c("type", "time")) %>% 
    dplyr::mutate(Observations= factor(variable, labels=c("0 obs per bin","1 obs per bin", "2+ obs per bin"))) %>% 
    ggplot(aes(x=time, y=value, linetype=Observations), linewidth=1)+
    geom_line()+
    #facet_wrap(~type)+ #color=factor(type))
    theme_classic()+
    xlab("Bin width (%)")+
    ylab("Mean proportions of individuals")#+


folder_path <- ifelse(dir.exists("E:/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/"),
                      "E:/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/",
                      "C:/Users/CISS Fondecyt/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/")

ggsave(plot_irregularity, file="_proposal_grant/2023/_figs/irreg240427.jpg", width=8.5, height=5.5, dpi=500)
ggsave(plot_irregularity_05_2024, file= paste0(folder_path,"_figs/irreg240513.jpg"), width=8.5, height=5.5, dpi=500)

The AUCs that suggest that although low values, we can suspect a share of a counting process behind visits rather than a perfect repeated measures. Thus, it seems reasonable to conduct an analysis that consider irregularity in observations and an extent of informative assessment times, conditional on a bulk of covariates, including past observed outcomes, past assessment history, and baseline covariates. The graphic show the mean proportions of individuals with 0,1, and >1 visits per bin as bin width varies from 1 to 50% of the gap between admissions. In the graphic, we can see that in the first 5 bins there were more than one observation in the same bin of time.


IIW

Code
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#load("C:/Users/CISS Fondecyt/Mi unidad/Alvacast/SISTRAT 2022 (github)/an_grant_23_24_04_15.RData")
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

# formulaph: the formula for the proportional hazards model for the visit intensity that will be used to derive inverse-intensity weights. The formula should usually use the counting process format (i.e. Surv(start,stop,event)). If a frailty model is used, the cluster(id) term should appear before other covariates
# 
# formulanull: if stabilised weights are to be used, the formula for the null model used to stabilise the weights
# 
# data: data frame containing the variables in the model
# 
# id: character string indicating which column of the data identifies subjects
# 
# time: character string indicating which column of the data contains the time at which the visit occurred
# 
# event: character string indicating which column of the data indicates whether or not a visit occurred. If every row corresponds to a visit, then this column will consist entirely of ones
# 
# lagvars: a vector of variable names corresponding to variables which need to be lagged by one visit to fit the visit intensity model. Typically time will be one of these variables. The function will internally add columns to the data containing the values of the lagged variables from the previous visit. Values of lagged variables for a subject's first visit will be set to NA. To access these variables in specifying the proportional hazards formulae, add ".lag" to the variable you wish to lag. For example, if time is the variable for time, time.lag is the time of the previous visit
# 
# invariant: a vector of variable names corresponding to variables in data that are time-invariant. It is not necessary to list every such variable, just those that are invariant and also included in the proportional hazards model
# 
# maxfu: the maximum follow-up time(s). If everyone is followed for the same length of time, this can be given as a single value. If individuals have different follow-up times, maxfu should have the same number of elements as there are rows of data
# 
# lagfirst: A vector giving the value of each lagged variable for the first time within each subject. This is helpful if, for example, time is the variable to be lagged and you know that all subjects entered the study at time zero
# 
# first: logical variable. If TRUE, the first observation for each individual is assigned an intensity of 1. This is appropriate if the first visit is a baseline visit at which recruitment to the study occurred; in this case the baseline visit is observed with probability 1.

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

invisible("2024-03-06: No es necesario correr el proceso longitudinal con ...")
invisible("fech_ing_num.lag,fech_ing_num, porque ya corregi time y no debiese tener ...")
invisible("superposiciones con lag.time")


invisible("Por lo visto si hay un problema, pq no recupero ninguno")

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

invisible("2014-03-08: Primero tenemos que ver si es plausible ver evidencia en contra de eventos aleatorios. ")

# glimpse(Phenobarb)

invisible("Correct id so it resemble Phenobarb")
if(class(Base_fiscalia_v15f_grant_23_24_long2_miss_proc$id)!="factor"){
Base_fiscalia_v15f_grant_23_24_long2_miss_proc$id <- factor(Base_fiscalia_v15f_grant_23_24_long2_miss_proc$id)
}
invisible("Correct id so it resemble Phenobarb")
data_mine_miss <- Base_fiscalia_v15f_grant_23_24_long2_miss_proc

invisible("Correct id so it resemble Phenobarb")
if(class(Base_fiscalia_v15f_grant_23_24_long2_miss_proc_multtr$id)!="factor"){
Base_fiscalia_v15f_grant_23_24_long2_miss_proc_multtr$id <- factor(Base_fiscalia_v15f_grant_23_24_long2_miss_proc_multtr$id)
}
data_mine_miss_restr <- Base_fiscalia_v15f_grant_23_24_long2_miss_proc_multtr
 
 invisible("2024-04-15: Need to take into account that this implies having only those who experiment the event, which might be bad. I have to have people that is observed. This should be the event")
# BAD: data_mine_miss <- data_mine_miss[data_mine_miss$event==1,]
 
invisible("2024-04-15: keep the tr. outcome")
data_mine_miss$tr_outcome <- data_mine_miss$event
data_mine_miss_restr$tr_outcome <- data_mine_miss_restr$event

invisible("2024-04-15: transform the event into 1 (being observed")
data_mine_miss$event<-1
data_mine_miss_restr$event<-1


data_mine_miss_proc <-  
cbind.data.frame(data_mine_miss,model.matrix(~ comp_bpsc_y - 1 + less_90d_tr -1, data = data.frame(data_mine_miss))) %>% dplyr::rename("comp_bpsc_y2_moderate"="comp_bpsc_y2-Moderate","comp_bpsc_y3_severe"="comp_bpsc_y3-Severe") %>% data.frame() 

data_mine_miss_restr_proc <-  
cbind.data.frame(data_mine_miss_restr,model.matrix(~ comp_bpsc_y - 1 + less_90d_tr -1, data = data.frame(data_mine_miss_restr))) %>% dplyr::rename("comp_bpsc_y2_moderate"="comp_bpsc_y2-Moderate","comp_bpsc_y3_severe"="comp_bpsc_y3-Severe") %>% data.frame() 

invisible("2024-04-18: eliminate Mild level")
data_mine_miss_proc$comp_bpsc_y1.Mild<-NULL
data_mine_miss_restr_proc$comp_bpsc_y1.Mild<-NULL

invisible("2024-04-15: following the addcensoredrows function vignette [https://search.r-project.org/CRAN/refmans/IrregLong/html/addcensoredrows.html] we crated a database to compute")
maxfu_df<-
  data_mine_miss_proc %>% 
  dplyr::group_by(id) %>% 
  dplyr::summarise(maxfu.time= max(cens_time, na.rm=T)) %>% #,0)
  dplyr::rename("maxfu.id"="id") %>% 
  as.data.frame()

maxfu_restr_df<-
  data_mine_miss_restr_proc  %>% 
  dplyr::group_by(id) %>% 
  dplyr::summarise(maxfu.time= max(cens_time, na.rm=T)) %>% #,0)
  dplyr::rename("maxfu.id"="id") %>% 
  as.data.frame()

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_ 
invisible("2024-04-15: Make a sample of 10% to test quicker")
data_mine_miss_proc_sample<-
data_mine_miss_proc %>% 
  dplyr::filter(id %in%
as.character(sample(1:length(unique(data_mine_miss_proc$id)),5e3)))

maxfu_sample_df<-
  data_mine_miss_proc_sample %>% 
  dplyr::group_by(id) %>% 
  dplyr::summarise(maxfu.time= round(max(cens_time, na.rm=T),0)) %>% 
  dplyr::rename("maxfu.id"="id") %>% 
  as.data.frame()

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
invisible("i0: only events; missing values, only event.lag")
i0 <- iiw.weights(
  Surv(time.lag,time,event)~ tr_outcome.lag + 
  cluster(id),
      id= "id",
      time= "time",
      event= "event", #event: dropout; 2024-04-18 being reobserved (visit)
      data= data_mine_miss_proc,
      invariant= "id",
      lagvars= c("time", "tr_outcome"),#"less_90d_tr", "comp_bpsc_y"), #
      maxfu= maxfu_df,#12*12,# Base_fiscalia_v15f_grant_23_24_long2$cens_time, #12*12,
      lagfirst= c(0,0),
      first= T  #If TRUE, the first observation for each individual is assigned an intensity of 1. This is appropriate if the first visit is a baseline visit at which recruitment to the study occurred
      )

i0$m
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag, 
    data = datacox, cluster = id)

                    coef exp(coef)  se(coef) robust se      z      p
tr_outcome.lag -1.868626  0.154336  0.009581  0.009747 -191.7 <2e-16

Likelihood ratio test=50186  on 1 df, p=< 2.2e-16
n= 162323, number of events= 89925 
   (150 observations deleted due to missingness)
Code
broom::tidy(i0$m)
# A tibble: 1 x 6
  term           estimate std.error robust.se statistic p.value
  <chr>             <dbl>     <dbl>     <dbl>     <dbl>   <dbl>
1 tr_outcome.lag    -1.87   0.00958   0.00975     -192.       0
Code
#broom::tidy(i0$m) %>% dput()

i0_df_out<-
  cbind.data.frame(term = "tr_outcome.lag", estimate = -1.86862564229299, 
    std.error = 0.00958080874903184, robust.se = 0.00974694287151046, 
    statistic = -191.714024276765, p.value = 0)%>% 
    dplyr::mutate(lo_95ci= estimate- 1.96 * robust.se) %>% 
    dplyr::mutate(up_95ci= estimate+ 1.96 * robust.se) %>% 
    dplyr::mutate(HR= estimate) %>% 
    dplyr::mutate_at(c("HR","lo_95ci","up_95ci"),~exp(.))

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

i0$datacox %>% 
  dplyr::group_by(id) %>% 
  dplyr::mutate(lag_time= lag(time)) %>% 
  dplyr::ungroup() %>% 
  dplyr::filter(dplyr::case_when(time <=lag_time & !is.na(lag_time)~T,T~F)) %>% 
  dplyr::select(hash_key, time, lag_time, motivodeegreso_mod_imp, dias_treat_imp_sin_na, enter_time, fech_ing_num, fech_egres_num, cens_time)%>%
  {
    if (nrow(.) > 0) {
      stop("There are ties or previous events that occured later than the actual events")
    } else {
      # Behavior when the condition isn't met (e.g., return an empty data frame)
      data.frame(.) 
    }  
  }
[1] hash_key               time                   lag_time              
[4] motivodeegreso_mod_imp dias_treat_imp_sin_na  enter_time            
[7] fech_ing_num           fech_egres_num         cens_time             
<0 rows> (or 0-length row.names)
Code
invisible("it seems that there are no errors")


#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
invisible("i0b: only events; missing values, only event.lag; lag first 1s")
i0b <- iiw.weights(
  Surv(time.lag,time,event)~ tr_outcome.lag + #comp_bpsc_y.lag+ less_90d_tr.lag+ 
  cluster(id),
      id= "id",
      time= "time",
      event= "event", #event: dropout
      data= data_mine_miss_proc,
      invariant= "id",
      lagvars= c("time", "tr_outcome"),#"less_90d_tr", "comp_bpsc_y"), #
      maxfu= maxfu_df,#Base_fiscalia_v15f_grant_23_24_long2$cens_time,
      lagfirst= c(1,1),
      first= T  #If TRUE, the first observation for each individual is assigned an intensity of 1. This is appropriate if the first visit is a baseline visit at which recruitment to the study occurred
      )

i0b$m
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag, 
    data = datacox, cluster = id)

                  coef exp(coef) se(coef) robust se     z      p
tr_outcome.lag 1.06837   2.91063  0.01705   0.01690 63.21 <2e-16

Likelihood ratio test=5298  on 1 df, p=< 2.2e-16
n= 159556, number of events= 87158 
   (2917 observations deleted due to missingness)
Code
broom::tidy(i0b$m)
# A tibble: 1 x 6
  term           estimate std.error robust.se statistic p.value
  <chr>             <dbl>     <dbl>     <dbl>     <dbl>   <dbl>
1 tr_outcome.lag     1.07    0.0170    0.0169      63.2       0
Code
i0b_df_out<-
  cbind.data.frame(term = "tr_outcome.lag", estimate = 1.06836854522169, 
    std.error = 0.0170488969088114, robust.se = 0.0169029487257102, 
    statistic = 63.2060454396721, p.value = 0)%>% 
    dplyr::mutate(lo_95ci= estimate- 1.96 * robust.se) %>% 
    dplyr::mutate(up_95ci= estimate+ 1.96 * robust.se) %>% 
    dplyr::mutate(HR= estimate) %>% 
    dplyr::mutate_at(c("HR","lo_95ci","up_95ci"),~exp(.))

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

summary(i0$iiw.weight)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  1.000   1.000   1.000   1.846   1.000   6.479 
Code
#  Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
# 1.000   1.000   1.000   1.846   1.000   6.479 
ploti0<-
ggplot( data= as.data.frame(i0$iiw.weight),aes(x= `i0$iiw.weight`))+
    geom_histogram(bins=30)+
  theme_classic()+ xlim(0,10)
#Removed 13708 rows containing non-finite values (`stat_bin()`). 


summary(i0b$iiw.weight)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 0.3436  1.0000  1.0000  0.8986  1.0000  1.0000 
Code
#   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
# 0.3436  1.0000  1.0000  0.8986  1.0000  1.0000
ploti0b<-
ggplot( data= as.data.frame(i0b$iiw.weight),aes(x= `i0b$iiw.weight`))+
    geom_histogram(bins=60)+
  theme_classic()+ xlim(0,10)

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
invisible("Restrict to users with more than one treatment")

data_mine_miss_proc_multtr<-
data_mine_miss_proc %>% 
  dplyr::group_by(hash_key) %>% dplyr::mutate(n=n()) %>% dplyr::ungroup() %>% dplyr::filter(case_when(n>1~ T,T~F)) %>% data.frame()
    #dplyr::filter(max_treatment>1) 

invisible("2014-04-15: make a follow-up for this subsample")
maxfu_multtr_df<-
    data_mine_miss_proc_multtr %>% 
    dplyr::group_by(id) %>% 
    dplyr::summarise(maxfu.time= max(cens_time, na.rm=T)) %>% #,0)
    dplyr::rename("maxfu.id"="id") %>% 
    as.data.frame()

invisible("Remember that his has only events (observations)")

paste0("The original database had ",
       format(length(unique(data_mine_miss_proc$hash_key)), big.mark=","),
       " patients and ",
       format(length(data_mine_miss_proc$hash_key), big.mark=","),
       " observations with only events, but keeping patients with more than one treatment left only ",
       format(length(unique(data_mine_miss_proc_multtr$hash_key)), big.mark=","),
       " patients and ",
       format(length(data_mine_miss_proc_multtr$hash_key), big.mark=","),
       " observations"
       )
[1] "The original database had 72,404 patients and 90,075 observations with only events, but keeping patients with more than one treatment left only 13,317 patients and 30,988 observations"
Code
#[1] "The original database had 72,404 patients and 90,075 observations with only events, but keeping patients with more than one treatment left only 13,317 patients and 30,988 observations"

invisible("multiple treatments; only event.lag; lag first 0s")
i0c <- iiw.weights(
  Surv(time.lag,time,event)~ tr_outcome.lag + #comp_bpsc_y.lag+ less_90d_tr.lag+ 
  cluster(id),
      id= "id",
      time= "time",
      event= "event", #event: dropout
      data= data_mine_miss_proc_multtr,
      invariant= "id",
      lagvars= c("time", "tr_outcome"),#"less_90d_tr", "comp_bpsc_y"), #
      maxfu= maxfu_multtr_df, #12*12,#Base_fiscalia_v15f_grant_23_24_long2$cens_time,
      lagfirst= c(0,0),
      first= T  #If TRUE, the first observation for each individual is assigned an intensity of 1. This is appropriate if the first visit is a baseline visit at which recruitment to the study occurred
      )
# In Surv(fech_ing_num.lag, fech_ing_num, event) :
#   Stop time must be > start time, NA created

i0c$m
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag, 
    data = datacox, cluster = id)

                    coef exp(coef)  se(coef) robust se      z      p
tr_outcome.lag -0.747183  0.473699  0.012291  0.008773 -85.17 <2e-16

Likelihood ratio test=3636  on 1 df, p=< 2.2e-16
n= 44272, number of events= 30959 
   (29 observations deleted due to missingness)
Code
broom::tidy(i0c$m)
# A tibble: 1 x 6
  term           estimate std.error robust.se statistic p.value
  <chr>             <dbl>     <dbl>     <dbl>     <dbl>   <dbl>
1 tr_outcome.lag   -0.747    0.0123   0.00877     -85.2       0
Code
i0c_df_out<-
  cbind.data.frame(term = "tr_outcome.lag", estimate = -0.747182521538917, 
    std.error = 0.0122906036735641, robust.se = 0.00877334474470237, 
    statistic = -85.1650702532908, p.value = 0)%>% 
    dplyr::mutate(lo_95ci= estimate- 1.96 * robust.se) %>% 
    dplyr::mutate(up_95ci= estimate+ 1.96 * robust.se) %>% 
    dplyr::mutate(HR= estimate) %>% 
    dplyr::mutate_at(c("HR","lo_95ci","up_95ci"),~exp(.))

i0d <- iiw.weights(
  Surv(time.lag,time,event)~ tr_outcome.lag + #comp_bpsc_y.lag+ less_90d_tr.lag+ 
  cluster(id),
      id= "id",
      time= "time",
      event= "event", #event: observed
      data= data_mine_miss_proc_multtr,
      invariant= "id",
      lagvars= c("time", "tr_outcome"),#"less_90d_tr", "comp_bpsc_y"), #
      maxfu= maxfu_multtr_df,#12*12,#Base_fiscalia_v15f_grant_23_24_long2$cens_time,
      lagfirst= c(1,1),
      first= T  #If TRUE, the first observation for each individual is assigned an intensity of 1. This is appropriate if the first visit is a baseline visit at which recruitment to the study occurred
      )
# In Surv(fech_ing_num.lag, fech_ing_num, event) :
#   Stop time must be > start time, NA created

i0d$m
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag, 
    data = datacox, cluster = id)

                  coef exp(coef) se(coef) robust se     z      p
tr_outcome.lag 0.27465   1.31607  0.01798   0.01291 21.27 <2e-16

Likelihood ratio test=247.4  on 1 df, p=< 2.2e-16
n= 43638, number of events= 30325 
   (663 observations deleted due to missingness)
Code
broom::tidy(i0d$m)
# A tibble: 1 x 6
  term           estimate std.error robust.se statistic   p.value
  <chr>             <dbl>     <dbl>     <dbl>     <dbl>     <dbl>
1 tr_outcome.lag    0.275    0.0180    0.0129      21.3 2.28e-100
Code
i0d_df_out<-
cbind.data.frame(term = "tr_outcome.lag", estimate = 0.274653363170145, 
    std.error = 0.0179832241105765, robust.se = 0.0129143329186389, 
    statistic = 21.2673287037339, p.value = 2.27894355540563e-100)%>% 
  dplyr::mutate(lo_95ci= estimate- 1.96 * robust.se) %>% 
  dplyr::mutate(up_95ci= estimate+ 1.96 * robust.se) %>% 
  dplyr::mutate(HR= estimate) %>% 
  dplyr::mutate_at(c("HR","lo_95ci","up_95ci"),~exp(.))

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

summary(i0c$iiw.weight)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  1.000   1.000   1.000   1.499   2.111   2.111 
Code
#  Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
# 1.000   1.000   1.000   1.499   2.111   2.111 

ploti0c<-
ggplot( data= as.data.frame(i0c$iiw.weight),aes(x= `i0c$iiw.weight`))+
    geom_histogram(bins=60)+
  theme_classic()+ xlim(0,10)

summary(i0d$iiw.weight)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 0.7598  0.7598  0.7598  0.7890  0.7598  1.0000 
Code
 #   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 # 0.7598  0.7598  0.7598  0.7890  0.7598  1.0000 

ploti0d<-
ggplot( data= as.data.frame(i0d$iiw.weight),aes(x= `i0d$iiw.weight`))+
    geom_histogram(bins=60)+
  theme_classic()+ xlim(0,10)

#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:

invisible("Now we use the 'data_mine_miss_proc' and try to dummify previous events")
 
i1 <- iiw.weights(
  Surv(time.lag,time,event)~ tr_outcome.lag+ comp_bpsc_y2_moderate.lag+ comp_bpsc_y3_severe.lag+ less_90d_tr1.lag+ policonsumo2.lag+ #less_90d_tr.lag+ 
  cluster(id),
      id= "id",
      time= "time",
      event= "event",
      data= data_mine_miss_proc,
      invariant= "id",
      lagvars= c("time", "tr_outcome", "comp_bpsc_y2_moderate", "comp_bpsc_y3_severe", "less_90d_tr1", "policonsumo2"),
      maxfu= maxfu_df,#Base_fiscalia_v15f_grant_23_24_long2$cens_time,
      lagfirst= rep(0),
      first= T  #If TRUE, the first observation for each individual is assigned an intensity of 1. This is appropriate if the first visit is a baseline visit at which recruitment to the study occurred
      )
invisible("No longer  Error in as.character.factor(x) : malformed factor")
# Además: Warning message:
# In Surv(time.lag, time, event) : Stop time must be > start time, NA created

invisible("Glimpse")
data_mine_miss_proc[,c("id", "time", "tr_outcome", "comp_bpsc_y2_moderate", "comp_bpsc_y3_severe", "less_90d_tr1")] %>% glimpse()
Rows: 90,075
Columns: 6
$ id                    <fct> 1, 2, 3, 4, 5, 6, 7, 7, 8, 9, 10, 11, 12, 13, 14~
$ time                  <dbl> 9.8032787, 8.5901639, 8.8196721, 1.1475410, 7.01~
$ tr_outcome            <dbl> 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, ~
$ comp_bpsc_y2_moderate <dbl> 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, ~
$ comp_bpsc_y3_severe   <dbl> 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, ~
$ less_90d_tr1          <dbl> 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, ~
Code
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
 
i1b <- iiw.weights(
  Surv(time.lag,time,event)~ tr_outcome.lag+ comp_bpsc_y2_moderate.lag+ comp_bpsc_y3_severe.lag+ less_90d_tr1.lag+ policonsumo2.lag+ #less_90d_tr.lag+ 
  cluster(id),
      id= "id",
      time= "time",
      event= "event",
      data= data_mine_miss_proc,
      invariant= "id",
      lagvars= c("time", "tr_outcome", "comp_bpsc_y2_moderate", "comp_bpsc_y3_severe", "less_90d_tr1","policonsumo2"),
      maxfu= maxfu_df,#12*12,#Base_fiscalia_v15f_grant_23_24_long2$cens_time,
      lagfirst= c(1,1,0,1,1,1),
      first= T  #If TRUE, the first observation for each individual is assigned an intensity of 1. This is appropriate if the first visit is a baseline visit at which recruitment to the study occurred
      )
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:

i1$m
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    comp_bpsc_y2_moderate.lag + comp_bpsc_y3_severe.lag + less_90d_tr1.lag + 
    policonsumo2.lag, data = datacox, cluster = id)

                             coef exp(coef) se(coef) robust se      z        p
tr_outcome.lag            0.17918   1.19624  0.01920   0.01968  9.105  < 2e-16
comp_bpsc_y2_moderate.lag 0.15399   1.16648  0.03195   0.03228  4.771 1.83e-06
comp_bpsc_y3_severe.lag   0.52003   1.68207  0.03238   0.03320 15.662  < 2e-16
less_90d_tr1.lag          0.04020   1.04102  0.01800   0.01847  2.176   0.0295
policonsumo2.lag          0.20122   1.22289  0.01985   0.02087  9.640  < 2e-16

Likelihood ratio test=990.1  on 5 df, p=< 2.2e-16
n= 90069, number of events= 17671 
   (72404 observations deleted due to missingness)
Code
broom::tidy(i1$m)
# A tibble: 5 x 6
  term                      estimate std.error robust.se statistic  p.value
  <chr>                        <dbl>     <dbl>     <dbl>     <dbl>    <dbl>
1 tr_outcome.lag              0.179     0.0192    0.0197      9.10 8.65e-20
2 comp_bpsc_y2_moderate.lag   0.154     0.0320    0.0323      4.77 1.83e- 6
3 comp_bpsc_y3_severe.lag     0.520     0.0324    0.0332     15.7  2.74e-55
4 less_90d_tr1.lag            0.0402    0.0180    0.0185      2.18 2.95e- 2
5 policonsumo2.lag            0.201     0.0199    0.0209      9.64 5.40e-22
Code
i1_df_out<-
cbind.data.frame(term = c("tr_outcome.lag", "comp_bpsc_y2_moderate.lag", 
"comp_bpsc_y3_severe.lag", "less_90d_tr1.lag", "policonsumo2.lag"
), estimate = c(0.179303997546012, 0.155159976117794, 0.522643191348996, 
0.0396946478038892, 0.200990917537315), std.error = c(0.0192034667885392, 
0.0319537357893297, 0.0323780221201328, 0.0180015312824529, 0.0198507378783121
), robust.se = c(0.0196804675715693, 0.0322870898718578, 0.0332167392234256, 
0.0184789094834668, 0.020873477226514), statistic = c(9.11075902510759, 
4.80563521623034, 15.7343316522896, 2.14810553833841, 9.62900983656
), p.value = c(8.18076035588734e-20, 1.54260938373677e-06, 8.79807768652411e-56, 
0.0317053723627336, 6.03077841071276e-22))%>% 
  dplyr::mutate(lo_95ci= estimate- 1.96 * robust.se) %>% 
  dplyr::mutate(up_95ci= estimate+ 1.96 * robust.se) %>% 
  dplyr::mutate(HR= estimate) %>% 
  dplyr::mutate_at(c("HR","lo_95ci","up_95ci"),~exp(.))

invisible("It is not necessary to investigate violations of the counting process (progressive times)")
invisible("Does really PSU in the previous treatment protects?. 2024-04-15: now it make sense, every coeff is HR>1!")

i1b$m
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    comp_bpsc_y2_moderate.lag + comp_bpsc_y3_severe.lag + less_90d_tr1.lag + 
    policonsumo2.lag, data = datacox, cluster = id)

                              coef exp(coef) se(coef) robust se       z      p
tr_outcome.lag            0.204432  1.226827 0.018508  0.020385  10.029 <2e-16
comp_bpsc_y2_moderate.lag 0.002844  1.002849 0.031859  0.033560   0.085  0.932
comp_bpsc_y3_severe.lag   1.435238  4.200644 0.030608  0.031895  44.999 <2e-16
less_90d_tr1.lag          1.279551  3.595026 0.011541  0.011892 107.597 <2e-16
policonsumo2.lag          0.840131  2.316670 0.018508  0.021128  39.765 <2e-16

Likelihood ratio test=61818  on 5 df, p=< 2.2e-16
n= 159556, number of events= 87158 
   (2917 observations deleted due to missingness)
Code
broom::tidy(i1b$m)
# A tibble: 5 x 6
  term                      estimate std.error robust.se statistic  p.value
  <chr>                        <dbl>     <dbl>     <dbl>     <dbl>    <dbl>
1 tr_outcome.lag             0.204      0.0185    0.0204   10.0    1.14e-23
2 comp_bpsc_y2_moderate.lag  0.00284    0.0319    0.0336    0.0848 9.32e- 1
3 comp_bpsc_y3_severe.lag    1.44       0.0306    0.0319   45.0    0       
4 less_90d_tr1.lag           1.28       0.0115    0.0119  108.     0       
5 policonsumo2.lag           0.840      0.0185    0.0211   39.8    0       
Code
i1b_df_out<-cbind.data.frame(term = c("tr_outcome.lag", "comp_bpsc_y2_moderate.lag", 
"comp_bpsc_y3_severe.lag", "less_90d_tr1.lag", "policonsumo2.lag"
), estimate = c(0.204663142678526, 0.00379136822637489, 1.43645954073343, 
1.27844124422329, 0.840329282274339), std.error = c(0.0185085344588536, 
0.0318597956273076, 0.0306100046937583, 0.011541793220903, 0.0185082077607319
), robust.se = c(0.020386616410505, 0.0335664648273184, 0.03190467043629, 
0.0118920257355396, 0.0211189171816328), statistic = c(10.039093224566, 
0.112951073217852, 45.0234878182453, 107.504076484012, 39.7903583335785
), p.value = c(1.02613077760236e-23, 0.910069344802066, 0, 0, 
0))%>% 
  dplyr::mutate(lo_95ci= estimate- 1.96 * robust.se) %>% 
  dplyr::mutate(up_95ci= estimate+ 1.96 * robust.se) %>% 
  dplyr::mutate(HR= estimate) %>% 
  dplyr::mutate_at(c("HR","lo_95ci","up_95ci"),~exp(.))

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

summary(i1$iiw.weight)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 0.3904  1.0000  1.0000  0.9119  1.0000  1.0000 
Code
#  Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
# 0.3896  1.0000  1.0000  0.9118  1.0000  1.0000 

ploti1<-
ggplot( data= as.data.frame(i1$iiw.weight),aes(x= `i1$iiw.weight`))+
    geom_histogram(bins=60)+
  theme_classic()+ xlim(0,10)


summary(i1b$iiw.weight)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 0.0233  1.0000  1.0000  0.8543  1.0000  1.0000 
Code
#  Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#0.02329 1.00000 1.00000 0.85417 1.00000 1.00000

ploti1b<-
ggplot( data= as.data.frame(i1b$iiw.weight),aes(x= `i1b$iiw.weight`))+
    geom_histogram(bins=60)+
  theme_classic()+ xlim(0,10)

#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
 
i1c <- iiw.weights(
  Surv(time.lag,time,event)~ tr_outcome.lag+ comp_bpsc_y2_moderate.lag+ comp_bpsc_y3_severe.lag+ less_90d_tr1.lag+ policonsumo2.lag+ #less_90d_tr.lag+ 
  cluster(id),
      id= "id",
      time= "time",
      event= "event",
      data= data_mine_miss_proc_multtr,
      invariant= "id",
      lagvars= c("time", "tr_outcome", "comp_bpsc_y2_moderate", "comp_bpsc_y3_severe", "less_90d_tr1","policonsumo2"),
      maxfu= maxfu_multtr_df,#12*12,#Base_fiscalia_v15f_grant_23_24_long2$cens_time,
      lagfirst= rep(0,6),
      formulanull= Surv(time.lag,time,event)~ 1,
      first= T  #If TRUE, the first observation for each individual is assigned an intensity of 1. This is appropriate if the first visit is a baseline visit at which recruitment to the study occurred
      )

i1d <- iiw.weights(
  Surv(time.lag,time,event)~ tr_outcome.lag+ comp_bpsc_y2_moderate.lag+ comp_bpsc_y3_severe.lag+ less_90d_tr1.lag+ policonsumo2.lag+ #less_90d_tr.lag+ 
  cluster(id),
      id= "id",
      time= "time",
      event= "event",
      data= data_mine_miss_proc_multtr,
      invariant= "id",
      lagvars= c("time", "tr_outcome", "comp_bpsc_y2_moderate", "comp_bpsc_y3_severe", "less_90d_tr1","policonsumo2"),
      maxfu= maxfu_multtr_df,#Base_fiscalia_v15f_grant_23_24_long2$cens_time,
      lagfirst=c(1,1,0,1,1,1),
      formulanull= Surv(time.lag,time,event)~ 1,
      first= T  #If TRUE, the first observation for each individual is assigned an intensity of 1. This is appropriate if the first visit is a baseline visit at which recruitment to the study occurred
      )

#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
i1c$m
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    comp_bpsc_y2_moderate.lag + comp_bpsc_y3_severe.lag + less_90d_tr1.lag + 
    policonsumo2.lag, data = datacox, cluster = id)

                              coef exp(coef) se(coef) robust se       z
tr_outcome.lag            -0.30163   0.73961  0.01544   0.01298 -23.240
comp_bpsc_y2_moderate.lag -0.90364   0.40509  0.01902   0.01729 -52.268
comp_bpsc_y3_severe.lag   -0.81206   0.44394  0.01987   0.01813 -44.786
less_90d_tr1.lag          -0.11483   0.89152  0.01764   0.01581  -7.262
policonsumo2.lag          -0.30996   0.73347  0.01596   0.01333 -23.251
                                 p
tr_outcome.lag             < 2e-16
comp_bpsc_y2_moderate.lag  < 2e-16
comp_bpsc_y3_severe.lag    < 2e-16
less_90d_tr1.lag          3.83e-13
policonsumo2.lag           < 2e-16

Likelihood ratio test=7757  on 5 df, p=< 2.2e-16
n= 44272, number of events= 30959 
   (29 observations deleted due to missingness)
Code
broom::tidy(i1c$m)
# A tibble: 5 x 6
  term                      estimate std.error robust.se statistic   p.value
  <chr>                        <dbl>     <dbl>     <dbl>     <dbl>     <dbl>
1 tr_outcome.lag              -0.302    0.0154    0.0130    -23.2  1.80e-119
2 comp_bpsc_y2_moderate.lag   -0.904    0.0190    0.0173    -52.3  0        
3 comp_bpsc_y3_severe.lag     -0.812    0.0199    0.0181    -44.8  0        
4 less_90d_tr1.lag            -0.115    0.0176    0.0158     -7.26 3.83e- 13
5 policonsumo2.lag            -0.310    0.0160    0.0133    -23.3  1.40e-119
Code
i1c_df_out<-
cbind.data.frame(term = c("tr_outcome.lag", "comp_bpsc_y2_moderate.lag", 
"comp_bpsc_y3_severe.lag", "less_90d_tr1.lag", "policonsumo2.lag"
), estimate = c(-0.302245037062739, -0.903956210807159, -0.813321897718714, 
-0.114245961363377, -0.308855961637097), std.error = c(0.0154310846877583, 
0.0190313654021939, 0.0198660721579724, 0.0176396124863469, 0.015967581763868
), robust.se = c(0.0129727086353161, 0.0173025650006909, 0.0181205187074852, 
0.0158153935544036, 0.0133307124770294), statistic = c(-23.2985296717391, 
-52.2440580787336, -44.8840295825939, -7.22371915503593, -23.1687512703688
), p.value = c(4.58804176323613e-120, 0, 0, 5.05846724994571e-13, 
9.40892053149018e-119))%>% 
  dplyr::mutate(lo_95ci= estimate- 1.96 * robust.se) %>% 
  dplyr::mutate(up_95ci= estimate+ 1.96 * robust.se) %>% 
  dplyr::mutate(HR= estimate) %>% 
  dplyr::mutate_at(c("HR","lo_95ci","up_95ci"),~exp(.))

i1d$m
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    comp_bpsc_y2_moderate.lag + comp_bpsc_y3_severe.lag + less_90d_tr1.lag + 
    policonsumo2.lag, data = datacox, cluster = id)

                              coef exp(coef) se(coef) robust se      z        p
tr_outcome.lag             0.09184   1.09619  0.01891   0.01472  6.240 4.36e-10
comp_bpsc_y2_moderate.lag -0.04619   0.95486  0.03189   0.02286 -2.021   0.0433
comp_bpsc_y3_severe.lag    0.39616   1.48611  0.03135   0.02234 17.736  < 2e-16
less_90d_tr1.lag           0.44917   1.56700  0.01418   0.01230 36.508  < 2e-16
policonsumo2.lag           0.23612   1.26632  0.01918   0.01518 15.553  < 2e-16

Likelihood ratio test=3294  on 5 df, p=< 2.2e-16
n= 43638, number of events= 30325 
   (663 observations deleted due to missingness)
Code
broom::tidy(i1d$m)
# A tibble: 5 x 6
  term                      estimate std.error robust.se statistic   p.value
  <chr>                        <dbl>     <dbl>     <dbl>     <dbl>     <dbl>
1 tr_outcome.lag              0.0918    0.0189    0.0147      6.24 4.36e- 10
2 comp_bpsc_y2_moderate.lag  -0.0462    0.0319    0.0229     -2.02 4.33e-  2
3 comp_bpsc_y3_severe.lag     0.396     0.0313    0.0223     17.7  2.23e- 70
4 less_90d_tr1.lag            0.449     0.0142    0.0123     36.5  8.35e-292
5 policonsumo2.lag            0.236     0.0192    0.0152     15.6  1.52e- 54
Code
i1d_df_out<-
cbind.data.frame(term = c("tr_outcome.lag", "comp_bpsc_y2_moderate.lag", 
"comp_bpsc_y3_severe.lag", "less_90d_tr1.lag", "policonsumo2.lag"
), estimate = c(0.0914594442348692, -0.0479067360824922, 0.395018880185181, 
0.449421044097101, 0.237239909387438), std.error = c(0.0189119890640763, 
0.0318880405199674, 0.0313463499911823, 0.0141743240908896, 0.0191755226262578
), robust.se = c(0.0147135065696911, 0.0228906898308705, 0.0223775814909527, 
0.0123024239813926, 0.0151689053922935), statistic = c(6.21601953291473, 
-2.09284807214001, 17.6524384614525, 36.5310970242003, 15.6398832514288
), p.value = c(5.09924589471951e-10, 0.0363627189586838, 9.74559709383605e-70, 
3.55949649474628e-292, 3.89441388490098e-55))%>% 
  dplyr::mutate(lo_95ci= estimate- 1.96 * robust.se) %>% 
  dplyr::mutate(up_95ci= estimate+ 1.96 * robust.se) %>% 
  dplyr::mutate(HR= estimate) %>% 
  dplyr::mutate_at(c("HR","lo_95ci","up_95ci"),~exp(.))


#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

summary(i1c$iiw.weight)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  1.000   1.000   3.046   2.660   4.152   5.104 
Code
  # Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  # 1.000   1.000   3.051   2.660   4.155   5.100 

ploti1c<-
ggplot( data= as.data.frame(i1c$iiw.weight),aes(x= `i1c$iiw.weight`))+
    geom_histogram(bins=60)+
  theme_classic()+ xlim(0,10)

summary(i1d$iiw.weight)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 0.3093  0.3093  0.3917  0.4846  0.6729  1.0473 
Code
 #   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 # 0.3094  0.3094  0.3922  0.4848  0.6737  1.0491 

ploti1d<-
ggplot( data= as.data.frame(i1d$iiw.weight),aes(x= `i1d$iiw.weight`))+
    geom_histogram(bins=60)+
  theme_classic()+ xlim(0,10)

#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:

invisible("Now we use the 'data_mine_miss_proc' and try to dummify previous events")
 
i2 <- iiw.weights(
  Surv(time.lag,time,event)~ tr_outcome.lag+ comp_bpsc_y2_moderate.lag+ comp_bpsc_y3_severe.lag+ less_90d_tr1.lag+ policonsumo2.lag+ treatment+#less_90d_tr.lag+ 
  cluster(hash_key),
      id= "id",
      time= "time",
      event= "event",
      data= data_mine_miss_proc,
      invariant= "id",
      lagvars= c("time", "tr_outcome", "comp_bpsc_y2_moderate", "comp_bpsc_y3_severe", "less_90d_tr1","policonsumo2", "treatment"),
      maxfu= maxfu_df,#12*12,#Base_fiscalia_v15f_grant_23_24_long2$cens_time,
      lagfirst= c(0,0,0,0,0,0,0),
      formulanull= Surv(time.lag,time,event)~ 1,
      first= T  #If TRUE, the first observation for each individual is assigned an intensity of 1. This is appropriate if the first visit is a baseline visit at which recruitment to the study occurred
      )

i2$m
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    comp_bpsc_y2_moderate.lag + comp_bpsc_y3_severe.lag + less_90d_tr1.lag + 
    policonsumo2.lag + treatment, data = datacox, cluster = hash_key)

                              coef exp(coef) se(coef) robust se       z
tr_outcome.lag            -0.20589   0.81392  0.01573   0.01912 -10.771
comp_bpsc_y2_moderate.lag -0.79538   0.45141  0.01931   0.02541 -31.300
comp_bpsc_y3_severe.lag   -0.64342   0.52549  0.02049   0.02735 -23.522
less_90d_tr1.lag           0.16432   1.17859  0.01785   0.02055   7.997
policonsumo2.lag          -0.49413   0.61010  0.01610   0.01925 -25.668
treatment                 -0.26432   0.76772  0.01253   0.01588 -16.644
                                 p
tr_outcome.lag             < 2e-16
comp_bpsc_y2_moderate.lag  < 2e-16
comp_bpsc_y3_severe.lag    < 2e-16
less_90d_tr1.lag          1.28e-15
policonsumo2.lag           < 2e-16
treatment                  < 2e-16

Likelihood ratio test=18907  on 6 df, p=< 2.2e-16
n= 89925, number of events= 89925 
   (72548 observations deleted due to missingness)
Code
broom::tidy(i2$m)
# A tibble: 6 x 6
  term                      estimate std.error robust.se statistic   p.value
  <chr>                        <dbl>     <dbl>     <dbl>     <dbl>     <dbl>
1 tr_outcome.lag              -0.206    0.0157    0.0191    -10.8  4.73e- 27
2 comp_bpsc_y2_moderate.lag   -0.795    0.0193    0.0254    -31.3  4.67e-215
3 comp_bpsc_y3_severe.lag     -0.643    0.0205    0.0274    -23.5  2.43e-122
4 less_90d_tr1.lag             0.164    0.0179    0.0205      8.00 1.28e- 15
5 policonsumo2.lag            -0.494    0.0161    0.0193    -25.7  2.64e-145
6 treatment                   -0.264    0.0125    0.0159    -16.6  3.36e- 62
Code
i2_df_out<-
cbind.data.frame(term = c("tr_outcome.lag", "comp_bpsc_y2_moderate.lag", 
"comp_bpsc_y3_severe.lag", "less_90d_tr1.lag", "policonsumo2.lag", 
"treatment"), estimate = c(-0.207821827365346, -0.795634053728106, 
-0.645100128805644, 0.165468256440595, -0.492699340028085, -0.263268962510958
), std.error = c(0.0157193638381784, 0.0193245384394512, 0.0204926882142567, 
0.0178456628682898, 0.0161016116581923, 0.0125485993898615), 
    robust.se = c(0.0190740908371374, 0.0254091936208088, 0.0272893127250465, 
    0.0205418969887765, 0.019247319556775, 0.0157928620459193
    ), statistic = c(-10.8955037039414, -31.3128415486796, -23.6392955478708, 
    8.05515948848355, -25.5983353201332, -16.6701236131537), 
    p.value = c(1.21094635824601e-27, 3.120318630146e-215, 1.52095150935808e-123, 
    7.93752420512666e-16, 1.5920600972545e-144, 2.1614709120083e-62
    ))%>% 
  dplyr::mutate(lo_95ci= estimate- 1.96 * robust.se) %>% 
  dplyr::mutate(up_95ci= estimate+ 1.96 * robust.se) %>% 
  dplyr::mutate(HR= estimate) %>% 
  dplyr::mutate_at(c("HR","lo_95ci","up_95ci"),~exp(.))

invisible("Does really previous dropout protect dropout?") 
invisible("maybe because lagfirst was 0")
invisible("Does really Severe previous biopsychosocial is a protective factor of dropout?")
invisible("Does really 0-90 days in previous treatment is a protective factor of dropout?")
invisible("Does really PSU in previous treatment is a protective factor of dropout?")
invisible("Does really more treatments protect dropout?")

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

summary(i2$iiw.weight)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   1.00    1.00    1.00    1.72    1.00   44.92 
Code
   # Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   # 1.00    1.00    1.00    1.72    1.00   44.54 

ploti2<-
ggplot( data= as.data.frame(i2$iiw.weight),aes(x= `i2$iiw.weight`))+
    geom_histogram(bins=60)+
  theme_classic()+ xlim(0,10)

invisible("Some extreme weights")

#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:

i3 <- iiw.weights(
  Surv(time.lag,time,event)~ tr_outcome.lag+ comp_bpsc_y2_moderate.lag+ comp_bpsc_y3_severe.lag+ less_90d_tr1.lag+ policonsumo2.lag+ policonsumo2+
  cluster(id),
      id= "id",
      time= "time",
      event= "event",
      data= data_mine_miss_proc,
      invariant= "id",
      lagvars= c("time", "tr_outcome", "comp_bpsc_y2_moderate", "comp_bpsc_y3_severe", "less_90d_tr1","policonsumo2"),
      maxfu= maxfu_df, #12*12,#Base_fiscalia_v15f_grant_23_24_long2$cens_time,
      lagfirst= c(0,0,0,0,0,0),
      first= T  #If TRUE, the first observation for each individual is assigned an intensity of 1. This is appropriate if the first visit is a baseline visit at which recruitment to the study occurred
      )

i3$m
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    comp_bpsc_y2_moderate.lag + comp_bpsc_y3_severe.lag + less_90d_tr1.lag + 
    policonsumo2.lag + policonsumo2, data = datacox, cluster = id)

                               coef exp(coef)  se(coef) robust se       z
tr_outcome.lag            -0.283314  0.753283  0.014943  0.018002 -15.738
comp_bpsc_y2_moderate.lag -0.916036  0.400102  0.017794  0.022331 -41.021
comp_bpsc_y3_severe.lag   -0.792492  0.452715  0.018677  0.023748 -33.370
less_90d_tr1.lag           0.116079  1.123085  0.017666  0.020221   5.741
policonsumo2.lag          -0.572257  0.564251  0.015723  0.018761 -30.502
policonsumo2               0.164367  1.178647  0.007740  0.007572  21.706
                                 p
tr_outcome.lag             < 2e-16
comp_bpsc_y2_moderate.lag  < 2e-16
comp_bpsc_y3_severe.lag    < 2e-16
less_90d_tr1.lag          9.44e-09
policonsumo2.lag           < 2e-16
policonsumo2               < 2e-16

Likelihood ratio test=18879  on 6 df, p=< 2.2e-16
n= 89925, number of events= 89925 
   (72548 observations deleted due to missingness)
Code
broom::tidy(i3$m)
# A tibble: 6 x 6
  term                      estimate std.error robust.se statistic   p.value
  <chr>                        <dbl>     <dbl>     <dbl>     <dbl>     <dbl>
1 tr_outcome.lag              -0.283   0.0149    0.0180     -15.7  8.36e- 56
2 comp_bpsc_y2_moderate.lag   -0.916   0.0178    0.0223     -41.0  0        
3 comp_bpsc_y3_severe.lag     -0.792   0.0187    0.0237     -33.4  3.68e-244
4 less_90d_tr1.lag             0.116   0.0177    0.0202       5.74 9.44e-  9
5 policonsumo2.lag            -0.572   0.0157    0.0188     -30.5  2.42e-204
6 policonsumo2                 0.164   0.00774   0.00757     21.7  1.80e-104
Code
i3_df_out<-
cbind.data.frame(term = c("tr_outcome.lag", "comp_bpsc_y2_moderate.lag", "comp_bpsc_y3_severe.lag", "less_90d_tr1.lag", "policonsumo2.lag", "policonsumo2"), estimate = c(-0.285082046756843, -0.916585948362177, 
-0.795077921451114, 0.11763808302626, -0.569791427138684, 0.164345823420033
), std.error = c(0.01492765131367, 0.0178053744947168, 0.0186611556090444, 
0.0176615698524595, 0.0157351197601704, 0.00773975706029995), 
    robust.se = c(0.0179877493992649, 0.0223101496182252, 0.0236349676291152, 
    0.0202282450263194, 0.0186724156095066, 0.00757289335696934
    ), statistic = c(-15.8486779212352, -41.0838100168282, -33.639898895892, 
    5.81553579528025, -30.5151427139716, 21.7018536605676), p.value = c(1.43569566146437e-56, 
    0, 4.38118085650704e-248, 6.04399775632908e-09, 1.64100047755937e-204, 
    1.97056906465211e-104))%>% 
  dplyr::mutate(lo_95ci= estimate- 1.96 * robust.se) %>% 
  dplyr::mutate(up_95ci= estimate+ 1.96 * robust.se) %>% 
  dplyr::mutate(HR= estimate) %>% 
  dplyr::mutate_at(c("HR","lo_95ci","up_95ci"),~exp(.))

invisible("Previous dropout prevents actual dropout?")
invisible("maybe because lagfirst was 0")
invisible("Severe biopsychosocial compromise is a risk factor of actual dropout?")
invisible("previous PSU prevents actual dropout?")

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

summary(i3$iiw.weight)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 0.7554  1.0000  1.0000  1.5860  1.0000  5.8803 
Code
 #   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 # 0.7543  1.0000  1.0000  1.5863  1.0000  5.8794 

ploti3<-
ggplot( data= as.data.frame(i3$iiw.weight),aes(x= `i3$iiw.weight`))+
    geom_histogram(bins=60)+
  theme_classic()+ xlim(0,10)

invisible("Reasonable weights")

#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:

i3b <- iiw.weights(
  Surv(time.lag,time,event)~ tr_outcome.lag+ comp_bpsc_y2_moderate.lag+ comp_bpsc_y3_severe.lag+ less_90d_tr1.lag+ policonsumo2.lag+ policonsumo2+
  cluster(id),
      id= "id",
      time= "time",
      event= "event",
      data= data_mine_miss_proc,
      invariant= "id",
      lagvars= c("time", "tr_outcome", "comp_bpsc_y2_moderate", "comp_bpsc_y3_severe", "less_90d_tr1","policonsumo2"),
      maxfu= maxfu_df,#12*12,#Base_fiscalia_v15f_grant_23_24_long2$cens_time,
      lagfirst= c(1,1,0,1,1,1),
      first= T  #If TRUE, the first observation for each individual is assigned an intensity of 1. This is appropriate if the first visit is a baseline visit at which recruitment to the study occurred
      )

i3b$m
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    comp_bpsc_y2_moderate.lag + comp_bpsc_y3_severe.lag + less_90d_tr1.lag + 
    policonsumo2.lag + policonsumo2, data = datacox, cluster = id)

                              coef exp(coef) se(coef) robust se      z        p
tr_outcome.lag            0.171974  1.187647 0.018876  0.017987  9.561  < 2e-16
comp_bpsc_y2_moderate.lag 0.008456  1.008491 0.031855  0.029226  0.289    0.772
comp_bpsc_y3_severe.lag   0.597821  1.818153 0.030962  0.028960 20.643  < 2e-16
less_90d_tr1.lag          0.855059  2.351514 0.012903  0.014455 59.154  < 2e-16
policonsumo2.lag          0.123359  1.131291 0.019003  0.020580  5.994 2.04e-09
policonsumo2              0.062995  1.065021 0.007827  0.007610  8.277  < 2e-16

Likelihood ratio test=13404  on 6 df, p=< 2.2e-16
n= 87158, number of events= 87158 
   (75315 observations deleted due to missingness)
Code
broom::tidy(i3b$m)
# A tibble: 6 x 6
  term                      estimate std.error robust.se statistic  p.value
  <chr>                        <dbl>     <dbl>     <dbl>     <dbl>    <dbl>
1 tr_outcome.lag             0.172     0.0189    0.0180      9.56  1.17e-21
2 comp_bpsc_y2_moderate.lag  0.00846   0.0319    0.0292      0.289 7.72e- 1
3 comp_bpsc_y3_severe.lag    0.598     0.0310    0.0290     20.6   1.13e-94
4 less_90d_tr1.lag           0.855     0.0129    0.0145     59.2   0       
5 policonsumo2.lag           0.123     0.0190    0.0206      5.99  2.04e- 9
6 policonsumo2               0.0630    0.00783   0.00761     8.28  1.26e-16
Code
i3b_df_out<-
cbind.data.frame(term = c("tr_outcome.lag", "comp_bpsc_y2_moderate.lag", "comp_bpsc_y3_severe.lag", "less_90d_tr1.lag", "policonsumo2.lag", "policonsumo2"), estimate = c(0.171524496903936, 0.00483620529764353, 0.596008396206245, 0.855542927694904, 0.125702439454889, 0.0627347379496959), std.error = c(0.018874296525738, 0.0318585598048639, 0.0309630894506515, 0.0128979749956164, 0.0189962053653323, 0.00782722711322035), robust.se = c(0.0179913060388931, 0.0292985300929012, 0.0290119903127091, 0.0144451828760062, 0.020549409265887, 0.00759613906581795), statistic = c(9.53374349439331, 0.165066482253842, 20.543519757938, 59.2268671873984, 6.11708287223425, 8.2587663819897), p.value = c(1.51711569984396e-21, 0.868891654364147, 8.79518334988573e-94, 0, 9.53038111046381e-10, 1.47185680747056e-16))%>% 
  dplyr::mutate(lo_95ci= estimate- 1.96 * robust.se) %>% 
  dplyr::mutate(up_95ci= estimate+ 1.96 * robust.se) %>% 
  dplyr::mutate(HR= estimate) %>% 
  dplyr::mutate_at(c("HR","lo_95ci","up_95ci"),~exp(.))

invisible("previous dropout transform to nothing; 2024-04-16: dropping out previously is a risk factor")
invisible("previous PSU still protects an actual dropout; 2024-04-16: previous PSU is a risk factor")
invisible("previous severe biopsuchosocial still is a risk factor; 2024-04-16: severe biopsychosocial, risk factor (80%+)")
invisible("now less than 90 days in treatment its a risk factor; 2024-04-16: less than 90 days tr. previously, risk factor of being observed posteriorly (readmission) (x2.3)")

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

summary(i3b$iiw.weight)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 0.1635  1.0000  1.0000  0.9053  1.0000  1.0000 
Code
#  Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
# 0.1634  1.0000  1.0000  0.9055  1.0000  1.0000 

ploti3b<-
ggplot( data= as.data.frame(i3b$iiw.weight),aes(x= `i3b$iiw.weight`))+
    geom_histogram(bins=60)+
  theme_classic()+ xlim(0,10)
invisible("Many 0s or near 0s")


#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:

i3_nolag <- iiw.weights(
  Surv(time.lag,time,event)~ tr_outcome.lag+ comp_bpsc_y2_moderate.lag+ comp_bpsc_y3_severe.lag+ less_90d_tr1.lag+ policonsumo2.lag+ policonsumo2+
  cluster(id),
      id= "id",
      time= "time",
      event= "event",
      data= data_mine_miss_proc,
      invariant= "id",
      lagvars= c("time", "tr_outcome", "comp_bpsc_y2_moderate", "comp_bpsc_y3_severe", "less_90d_tr1","policonsumo2"),
      maxfu= maxfu_df, #12*12,#Base_fiscalia_v15f_grant_23_24_long2$cens_time,
      #lagfirst= rep(1,6),
      first= T  #If TRUE, the first observation for each individual is assigned an intensity of 1. This is appropriate if the first visit is a baseline visit at which recruitment to the study occurred
      )

Error in shift(.SD, fill = lagfirst[col]): entrada en evaluacion: recursivo por defecto o problemas anteriores?

Code
invisible("Here, i didnt specified lagfirst")
# Error in shift(.SD, fill = lagfirst[col]) : 
#   entrada en evaluacion: recursivo por defecto o problemas anteriores?
invisible("It gives an error")


#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:

invisible("We format the data to include non-events; 2024-04-16: this is no longer useful because we are including every record (all event=1)")
# data_mine2_miss <- Base_fiscalia_v15f_grant_23_24_long2_miss_proc
# 
# data_mine2_miss_proc <-  
# data_mine2_miss %>% 
# dplyr::mutate(comp_bpsc_y2_moderate= ifelse(as.character(comp_bpsc_y)=="2-Moderate",1,0),
#           comp_bpsc_y3_severe= ifelse(as.character(comp_bpsc_y)=="3-Severe",1,0),
#           less_90d_tr1= ifelse(less_90d_tr==1,1,0)) %>% data.frame() 

i4 <- iiw.weights(
  Surv(time.lag,time,event)~ tr_outcome.lag+ comp_bpsc_y2_moderate.lag+ comp_bpsc_y3_severe.lag+ less_90d_tr1.lag+ policonsumo2.lag+ treatment.lag+
  cluster(hash_key),
      id= "id",
      time= "time",
      event= "event",
      data= data_mine_miss_proc,
      invariant= "id",
      lagvars= c("time", "tr_outcome", "comp_bpsc_y2_moderate", "comp_bpsc_y3_severe", "less_90d_tr1","policonsumo2", "treatment"),
      maxfu= maxfu_df,#12*12,#Base_fiscalia_v15f_grant_23_24_long2$cens_time,
      lagfirst= rep(0,7),
      first= T  #If TRUE, the first observation for each individual is assigned an intensity of 1. This is appropriate if the first visit is a baseline visit at which recruitment to the study occurred
      )

i4$m
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    comp_bpsc_y2_moderate.lag + comp_bpsc_y3_severe.lag + less_90d_tr1.lag + 
    policonsumo2.lag + treatment.lag, data = datacox, cluster = hash_key)

                              coef exp(coef) se(coef) robust se       z
tr_outcome.lag            -0.20589   0.81392  0.01573   0.01912 -10.771
comp_bpsc_y2_moderate.lag -0.79538   0.45141  0.01931   0.02541 -31.300
comp_bpsc_y3_severe.lag   -0.64342   0.52549  0.02049   0.02735 -23.522
less_90d_tr1.lag           0.16432   1.17859  0.01785   0.02055   7.997
policonsumo2.lag          -0.49413   0.61010  0.01610   0.01925 -25.668
treatment.lag             -0.26432   0.76772  0.01253   0.01588 -16.644
                                 p
tr_outcome.lag             < 2e-16
comp_bpsc_y2_moderate.lag  < 2e-16
comp_bpsc_y3_severe.lag    < 2e-16
less_90d_tr1.lag          1.28e-15
policonsumo2.lag           < 2e-16
treatment.lag              < 2e-16

Likelihood ratio test=18907  on 6 df, p=< 2.2e-16
n= 89925, number of events= 89925 
   (72548 observations deleted due to missingness)
Code
broom::tidy(i4$m)
# A tibble: 6 x 6
  term                      estimate std.error robust.se statistic   p.value
  <chr>                        <dbl>     <dbl>     <dbl>     <dbl>     <dbl>
1 tr_outcome.lag              -0.206    0.0157    0.0191    -10.8  4.73e- 27
2 comp_bpsc_y2_moderate.lag   -0.795    0.0193    0.0254    -31.3  4.67e-215
3 comp_bpsc_y3_severe.lag     -0.643    0.0205    0.0274    -23.5  2.43e-122
4 less_90d_tr1.lag             0.164    0.0179    0.0205      8.00 1.28e- 15
5 policonsumo2.lag            -0.494    0.0161    0.0193    -25.7  2.64e-145
6 treatment.lag               -0.264    0.0125    0.0159    -16.6  3.36e- 62
Code
i4_df_out<-
cbind.data.frame(term = c("tr_outcome.lag", "comp_bpsc_y2_moderate.lag", 
"comp_bpsc_y3_severe.lag", "less_90d_tr1.lag", "policonsumo2.lag", 
"treatment.lag"), estimate = c(-0.207821827365346, -0.795634053728106, 
-0.645100128805644, 0.165468256440595, -0.492699340028085, -0.263268962510958
), std.error = c(0.0157193638381784, 0.0193245384394512, 0.0204926882142567, 
0.0178456628682898, 0.0161016116581923, 0.0125485993898615), 
    robust.se = c(0.0190740908371374, 0.0254091936208087, 0.0272893127250465, 
    0.0205418969887765, 0.019247319556775, 0.0157928620459193
    ), statistic = c(-10.8955037039414, -31.3128415486796, -23.6392955478708, 
    8.05515948848355, -25.5983353201332, -16.6701236131537), 
    p.value = c(1.21094635824605e-27, 3.12031863014138e-215, 
    1.52095150935808e-123, 7.93752420512701e-16, 1.59206009725477e-144, 
    2.16147091200972e-62))%>% 
  dplyr::mutate(lo_95ci= estimate- 1.96 * robust.se) %>% 
  dplyr::mutate(up_95ci= estimate+ 1.96 * robust.se) %>% 
  dplyr::mutate(HR= estimate) %>% 
  dplyr::mutate_at(c("HR","lo_95ci","up_95ci"),~exp(.))

invisible("Previous dropout protects posterior dropout, but 95% CI does not replicate that; prev. dropouts protects posterior obs.")
invisible("Moderate previous biopsychosocial status protects actual dropout; both severe and moderate pro")
invisible("Severe previous biopsychosocial status protects actual dropout")
invisible("0-90 days in previous tr. predicts actual dropout")
invisible("previous PSU protects actual dropout")

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

summary(i4$iiw.weight)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   1.00    1.00    1.00    1.72    1.00   44.92 
Code
# Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
# 1.00    1.00    1.00    1.72    1.00   44.54  #2024-04-16: muy bizarro que haya un caso con probabilidades muy altas

ploti4<-
ggplot( data= as.data.frame(i4$iiw.weight),aes(x= `i4$iiw.weight`))+
    geom_histogram(bins=60)+
  theme_classic()+ xlim(0,10)
invisible("Reasonable, some positive outliers but very big")

#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:

i4b <- iiw.weights(
  Surv(time.lag,time,event)~ tr_outcome.lag+ comp_bpsc_y2_moderate.lag+ comp_bpsc_y3_severe.lag+ less_90d_tr1.lag+ policonsumo2.lag+ treatment.lag+
  cluster(id),
      id= "id",
      time= "time",
      event= "event",
      data= data_mine_miss_proc,
      invariant= "id",
      lagvars= c("time", "tr_outcome", "comp_bpsc_y2_moderate", "comp_bpsc_y3_severe", "less_90d_tr1","policonsumo2", "treatment"),
      maxfu= maxfu_df,#12*12,#Base_fiscalia_v15f_grant_23_24_long2$cens_time,
      lagfirst= c(1,1,0,1,1,1,1),
      first= T  #If TRUE, the first observation for each individual is assigned an intensity of 1. This is appropriate if the first visit is a baseline visit at which recruitment to the study occurred
      )

i4b$m
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    comp_bpsc_y2_moderate.lag + comp_bpsc_y3_severe.lag + less_90d_tr1.lag + 
    policonsumo2.lag + treatment.lag, data = datacox, cluster = id)

                               coef exp(coef)  se(coef) robust se       z
tr_outcome.lag             0.203911  1.226189  0.018495  0.020208  10.091
comp_bpsc_y2_moderate.lag -0.001234  0.998767  0.031861  0.033417  -0.037
comp_bpsc_y3_severe.lag    1.426373  4.163571  0.030614  0.031776  44.888
less_90d_tr1.lag           1.273223  3.572347  0.011538  0.011872 107.246
policonsumo2.lag           0.846049  2.330422  0.018511  0.020888  40.503
treatment.lag              0.145624  1.156762  0.011528  0.015317   9.507
                               p
tr_outcome.lag            <2e-16
comp_bpsc_y2_moderate.lag  0.971
comp_bpsc_y3_severe.lag   <2e-16
less_90d_tr1.lag          <2e-16
policonsumo2.lag          <2e-16
treatment.lag             <2e-16

Likelihood ratio test=61967  on 6 df, p=< 2.2e-16
n= 159556, number of events= 87158 
   (2917 observations deleted due to missingness)
Code
broom::tidy(i4b$m)
# A tibble: 6 x 6
  term                      estimate std.error robust.se statistic  p.value
  <chr>                        <dbl>     <dbl>     <dbl>     <dbl>    <dbl>
1 tr_outcome.lag             0.204      0.0185    0.0202   10.1    6.07e-24
2 comp_bpsc_y2_moderate.lag -0.00123    0.0319    0.0334   -0.0369 9.71e- 1
3 comp_bpsc_y3_severe.lag    1.43       0.0306    0.0318   44.9    0       
4 less_90d_tr1.lag           1.27       0.0115    0.0119  107.     0       
5 policonsumo2.lag           0.846      0.0185    0.0209   40.5    0       
6 treatment.lag              0.146      0.0115    0.0153    9.51   1.96e-21
Code
i4b_df_out<-
cbind.data.frame(term = c("tr_outcome.lag", "comp_bpsc_y2_moderate.lag", 
"comp_bpsc_y3_severe.lag", "less_90d_tr1.lag", "policonsumo2.lag", 
"treatment.lag"), estimate = c(0.204102531211081, -0.000387761814109535, 
1.42748577725764, 1.27218207100866, 0.846223498441861, 0.145344423972201
), std.error = c(0.0184955717163036, 0.0318615324506995, 0.0306160739199961, 
0.011538166117299, 0.0185111088069143, 0.0115312169046261), robust.se = c(0.0202100857453012, 
0.0334267319824861, 0.0317905279138096, 0.0118704233236429, 0.0208804874610975, 
0.0153330805627965), statistic = c(10.0990433085389, -0.0116003507107037, 
44.9028648133129, 107.172426485818, 40.5269991909175, 9.47914043606206
), p.value = c(5.5783718456834e-24, 0.990744466851805, 0, 0, 
0, 2.56386267293478e-21))%>% 
  dplyr::mutate(lo_95ci= estimate- 1.96 * robust.se) %>% 
  dplyr::mutate(up_95ci= estimate+ 1.96 * robust.se) %>% 
  dplyr::mutate(HR= estimate) %>% 
  dplyr::mutate_at(c("HR","lo_95ci","up_95ci"),~exp(.))

invisible("Previous dropout now predicts actual dropout")
invisible("Moderate previous biopsychosocial status does not [2024-04-16] predict actual dropout")
invisible("Severe previous biopsychosocial status predict actual dropout")
invisible("0-90 days in previous tr. predicts actual dropout")
invisible("previous PSU predicts posterior dropout")
invisible("more treatments can be a protective factor for subsequent treatments [2024-04-16]")

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

summary(i4b$iiw.weight)
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
0.008675 1.000000 1.000000 0.853592 1.000000 1.023063 
Code
#     Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
# 0.008687 1.000000 1.000000 0.853483 1.000000 1.022155 

ploti4b<-
ggplot( data= as.data.frame(i4b$iiw.weight),aes(x= `i4b$iiw.weight`))+
    geom_histogram(bins=60)+
  theme_classic()+ xlim(0,10)
invisible("Reasonable")

#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:

#2024-04-16: no longer useful
# 
# data_mine2_miss_proc_multtr<-
#     data_mine2_miss_proc %>% 
#     dplyr::group_by(hash_key) %>% dplyr::mutate(n=n()) %>% dplyr::ungroup() %>% dplyr::filter(case_when(n>1~ T,T~F)) %>% data.frame()
# 
# 
# paste0("The original database had ",
#        format(length(unique(data_mine2_miss_proc$hash_key)), big.mark=","),
#        " patients and ",
#        format(length(data_mine2_miss_proc$hash_key), big.mark=","),
#        " observations with only events, but keeping patients with more than one treatment left only ",
#        format(length(unique(data_mine2_miss_proc_multtr$hash_key)), big.mark=","),
#        " patients and ",
#        format(length(data_mine2_miss_proc_multtr$hash_key), big.mark=","),
#        " observations"
#        )

#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:

i4c <- iiw.weights(
  Surv(time.lag,time,event)~ tr_outcome.lag+ comp_bpsc_y2_moderate.lag+ comp_bpsc_y3_severe.lag+ less_90d_tr1.lag+ policonsumo2.lag+ treatment.lag+
  cluster(id),
      id= "id",
      time= "time",
      event= "event",
      data= data_mine_miss_proc_multtr,
      invariant= "id",
      lagvars= c("time", "tr_outcome", "comp_bpsc_y2_moderate", "comp_bpsc_y3_severe", "less_90d_tr1","policonsumo2", "treatment"),
      maxfu= maxfu_multtr_df,#12*12,#Base_fiscalia_v15f_grant_23_24_long2$cens_time,
      lagfirst= rep(0,7),# c(1,1,0,1,1,1,1),
      #formulanull= Surv(time.lag,time,event)~ 1,
      first= T  #If TRUE, the first observation for each individual is assigned an intensity of 1. This is appropriate if the first visit is a baseline visit at which recruitment to the study occurred
      )


invisible("FORMULANULL entrega ponderaciones y coeficientes iguales a sin formulanull")
i4c$m
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    comp_bpsc_y2_moderate.lag + comp_bpsc_y3_severe.lag + less_90d_tr1.lag + 
    policonsumo2.lag + treatment.lag, data = datacox, cluster = id)

                              coef exp(coef) se(coef) robust se       z
tr_outcome.lag             0.02895   1.02937  0.01743   0.01596   1.814
comp_bpsc_y2_moderate.lag -0.28716   0.75039  0.02330   0.02407 -11.928
comp_bpsc_y3_severe.lag   -0.12854   0.87938  0.02427   0.02515  -5.112
less_90d_tr1.lag           0.12332   1.13125  0.01801   0.01722   7.162
policonsumo2.lag          -0.18087   0.83454  0.01788   0.01620 -11.167
treatment.lag             -1.33538   0.26306  0.01551   0.02788 -47.895
                                 p
tr_outcome.lag              0.0697
comp_bpsc_y2_moderate.lag  < 2e-16
comp_bpsc_y3_severe.lag   3.19e-07
less_90d_tr1.lag          7.94e-13
policonsumo2.lag           < 2e-16
treatment.lag              < 2e-16

Likelihood ratio test=16880  on 6 df, p=< 2.2e-16
n= 44272, number of events= 30959 
   (29 observations deleted due to missingness)
Code
broom::tidy(i4c$m)
# A tibble: 6 x 6
  term                      estimate std.error robust.se statistic  p.value
  <chr>                        <dbl>     <dbl>     <dbl>     <dbl>    <dbl>
1 tr_outcome.lag              0.0290    0.0174    0.0160      1.81 6.97e- 2
2 comp_bpsc_y2_moderate.lag  -0.287     0.0233    0.0241    -11.9  8.48e-33
3 comp_bpsc_y3_severe.lag    -0.129     0.0243    0.0251     -5.11 3.19e- 7
4 less_90d_tr1.lag            0.123     0.0180    0.0172      7.16 7.94e-13
5 policonsumo2.lag           -0.181     0.0179    0.0162    -11.2  5.91e-29
6 treatment.lag              -1.34      0.0155    0.0279    -47.9  0       
Code
i4c_df_out<-
cbind.data.frame(term = c("tr_outcome.lag", "comp_bpsc_y2_moderate.lag", 
"comp_bpsc_y3_severe.lag", "less_90d_tr1.lag", "policonsumo2.lag", 
"treatment.lag"), estimate = c(0.0283175141261873, -0.286052899938312, 
-0.12762290937749, 0.123676646115759, -0.18046907845805, -1.33577820010306
), std.error = c(0.0174225204858456, 0.0232998089273985, 0.0242539101271994, 
0.0180040024564168, 0.0178722433414989, 0.0155176207475381), 
    robust.se = c(0.0159600395125907, 0.0240699432379503, 0.0251576808692291, 
    0.0172158438569811, 0.016189953421006, 0.0278980099478121
    ), statistic = c(1.77427594109952, -11.884236581302, -5.07292027595389, 
    7.18388521313221, -11.1469794733255, -47.8807700836676), 
    p.value = c(0.0760175181238697, 1.42935024171782e-32, 3.91756657551751e-07, 
    6.77577589606902e-13, 7.40784798532889e-29, 0))%>% 
  dplyr::mutate(lo_95ci= estimate- 1.96 * robust.se) %>% 
  dplyr::mutate(up_95ci= estimate+ 1.96 * robust.se) %>% 
  dplyr::mutate(HR= estimate) %>% 
  dplyr::mutate_at(c("HR","lo_95ci","up_95ci"),~exp(.))

invisible("Risk factors: previous dropout, <90 days previous treatment; protective: previous PSU!!!!, previous treatments")

i4d <- iiw.weights(
  Surv(time.lag,time,event)~ tr_outcome.lag+ comp_bpsc_y2_moderate.lag+ comp_bpsc_y3_severe.lag+ less_90d_tr1.lag+ policonsumo2.lag+ treatment.lag+
  cluster(id),
      id= "id",
      time= "time",
      event= "event",
      data= data_mine_miss_proc_multtr,
      invariant= "id",
      lagvars= c("time", "tr_outcome", "comp_bpsc_y2_moderate", "comp_bpsc_y3_severe", "less_90d_tr1","policonsumo2", "treatment"),
      maxfu= maxfu_multtr_df,#12*12,#Base_fiscalia_v15f_grant_23_24_long2$cens_time,
      lagfirst= c(1,1,0,1,1,1,1),# 
      #formulanull= Surv(time.lag,time,event)~ 1,
      first= T  #If TRUE, the first observation for each individual is assigned an intensity of 1. This is appropriate if the first visit is a baseline visit at which recruitment to the study occurred
      )

i4d$m
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    comp_bpsc_y2_moderate.lag + comp_bpsc_y3_severe.lag + less_90d_tr1.lag + 
    policonsumo2.lag + treatment.lag, data = datacox, cluster = id)

                               coef exp(coef)  se(coef) robust se       z
tr_outcome.lag             0.132783  1.142003  0.019040  0.016285   8.154
comp_bpsc_y2_moderate.lag  0.003703  1.003710  0.031880  0.025639   0.144
comp_bpsc_y3_severe.lag    0.479705  1.615598  0.031391  0.025947  18.488
less_90d_tr1.lag           0.589226  1.802592  0.014530  0.014917  39.500
policonsumo2.lag           0.103722  1.109292  0.019252  0.018027   5.754
treatment.lag             -1.346903  0.260044  0.016631  0.032231 -41.789
                                 p
tr_outcome.lag            3.52e-16
comp_bpsc_y2_moderate.lag    0.885
comp_bpsc_y3_severe.lag    < 2e-16
less_90d_tr1.lag           < 2e-16
policonsumo2.lag          8.72e-09
treatment.lag              < 2e-16

Likelihood ratio test=11563  on 6 df, p=< 2.2e-16
n= 43638, number of events= 30325 
   (663 observations deleted due to missingness)
Code
broom::tidy(i4d$m)
# A tibble: 6 x 6
  term                      estimate std.error robust.se statistic  p.value
  <chr>                        <dbl>     <dbl>     <dbl>     <dbl>    <dbl>
1 tr_outcome.lag             0.133      0.0190    0.0163     8.15  3.52e-16
2 comp_bpsc_y2_moderate.lag  0.00370    0.0319    0.0256     0.144 8.85e- 1
3 comp_bpsc_y3_severe.lag    0.480      0.0314    0.0259    18.5   2.58e-76
4 less_90d_tr1.lag           0.589      0.0145    0.0149    39.5   0       
5 policonsumo2.lag           0.104      0.0193    0.0180     5.75  8.72e- 9
6 treatment.lag             -1.35       0.0166    0.0322   -41.8   0       
Code
i4d_df_out<-
cbind.data.frame(term = c("tr_outcome.lag", "comp_bpsc_y2_moderate.lag", 
"comp_bpsc_y3_severe.lag", "less_90d_tr1.lag", "policonsumo2.lag", 
"treatment.lag"), estimate = c(0.132485185827427, 0.00411115497188839, 
0.481109354943001, 0.58947698342646, 0.105500181664414, -1.34747522983601
), std.error = c(0.0190393190327229, 0.0318822421857093, 0.0313896070698578, 
0.0145276202543287, 0.0192443206789934, 0.0166349222671111), 
    robust.se = c(0.0162768310200937, 0.0256183384356621, 0.0259237137593143, 
    0.0149119908677786, 0.0180095159064235, 0.0322392535580045
    ), statistic = c(8.13949506902635, 0.160477034145409, 18.5586586632535, 
    39.5304013161775, 5.85802429185703, -41.7961050931792), p.value = c(3.96929765929241e-16, 
    0.87250531125718, 6.94162601178333e-77, 0, 4.68406148676732e-09, 
    0))%>%
  dplyr::mutate(lo_95ci= estimate- 1.96 * robust.se) %>% 
  dplyr::mutate(up_95ci= estimate+ 1.96 * robust.se) %>% 
  dplyr::mutate(HR= estimate) %>% 
  dplyr::mutate_at(c("HR","lo_95ci","up_95ci"),~exp(.))

invisible("Razonable: abandono previo, compromiso severo previo, menos de 90 días y policonsumo previo son predictores de readmisiones; no así el recuento de tratamientos (esto con lag 1's)")

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_


summary(i4c$iiw.weight)
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
    0.19     0.19     0.86     4.91     1.14 49523.91 
Code
# Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
# 0.19     0.19     0.86     4.91     1.13 49631.97 

ploti4c<-
ggplot( data= as.data.frame(i4c$iiw.weight),aes(x= `i4c$iiw.weight`))+
    geom_histogram(bins=60)+
  theme_classic()+ xlim(0,10)
invisible("highly extreme values")


summary(i4d$iiw.weight)
     Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
    0.131     0.131     0.210     1.515     0.379 18136.282 
Code
#  Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
# 0.130     0.130     0.210     1.516     0.378 18179.444 

ploti4d<-
ggplot( data= as.data.frame(i4d$iiw.weight),aes(x= `i4d$iiw.weight`))+
    geom_histogram(bins=60)+
  theme_classic()+ xlim(0,10)
invisible(" highly extreme values")

#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:

i5 <- iiw.weights(
  Surv(time.lag,time,event)~ tr_outcome.lag+ comp_bpsc_y2_moderate.lag+ comp_bpsc_y3_severe.lag+ less_90d_tr1.lag+ policonsumo2.lag+ policonsumo2+
  cluster(id),
      id= "id",
      time= "time",
      event= "event",
      data= data_mine_miss_proc,
      invariant= "id",
      lagvars= c("time", "tr_outcome", "comp_bpsc_y2_moderate", "comp_bpsc_y3_severe", "less_90d_tr1","policonsumo2"),
      maxfu= maxfu_df,#12*12,#Base_fiscalia_v15f_grant_23_24_long2$cens_time,
      lagfirst= rep(0,6),
      first= T  #If TRUE, the first observation for each individual is assigned an intensity of 1. This is appropriate if the first visit is a baseline visit at which recruitment to the study occurred
      )

i5$m
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    comp_bpsc_y2_moderate.lag + comp_bpsc_y3_severe.lag + less_90d_tr1.lag + 
    policonsumo2.lag + policonsumo2, data = datacox, cluster = id)

                               coef exp(coef)  se(coef) robust se       z
tr_outcome.lag            -0.283314  0.753283  0.014943  0.018002 -15.738
comp_bpsc_y2_moderate.lag -0.916036  0.400102  0.017794  0.022331 -41.021
comp_bpsc_y3_severe.lag   -0.792492  0.452715  0.018677  0.023748 -33.370
less_90d_tr1.lag           0.116079  1.123085  0.017666  0.020221   5.741
policonsumo2.lag          -0.572257  0.564251  0.015723  0.018761 -30.502
policonsumo2               0.164367  1.178647  0.007740  0.007572  21.706
                                 p
tr_outcome.lag             < 2e-16
comp_bpsc_y2_moderate.lag  < 2e-16
comp_bpsc_y3_severe.lag    < 2e-16
less_90d_tr1.lag          9.44e-09
policonsumo2.lag           < 2e-16
policonsumo2               < 2e-16

Likelihood ratio test=18879  on 6 df, p=< 2.2e-16
n= 89925, number of events= 89925 
   (72548 observations deleted due to missingness)
Code
broom::tidy(i5$m)
# A tibble: 6 x 6
  term                      estimate std.error robust.se statistic   p.value
  <chr>                        <dbl>     <dbl>     <dbl>     <dbl>     <dbl>
1 tr_outcome.lag              -0.283   0.0149    0.0180     -15.7  8.36e- 56
2 comp_bpsc_y2_moderate.lag   -0.916   0.0178    0.0223     -41.0  0        
3 comp_bpsc_y3_severe.lag     -0.792   0.0187    0.0237     -33.4  3.68e-244
4 less_90d_tr1.lag             0.116   0.0177    0.0202       5.74 9.44e-  9
5 policonsumo2.lag            -0.572   0.0157    0.0188     -30.5  2.42e-204
6 policonsumo2                 0.164   0.00774   0.00757     21.7  1.80e-104
Code
i5_df_out<-
cbind.data.frame(term = c("tr_outcome.lag", "comp_bpsc_y2_moderate.lag", 
"comp_bpsc_y3_severe.lag", "less_90d_tr1.lag", "policonsumo2.lag", 
"policonsumo2"), estimate = c(-0.285082046756843, -0.916585948362177, 
-0.795077921451114, 0.11763808302626, -0.569791427138684, 0.164345823420033
), std.error = c(0.01492765131367, 0.0178053744947168, 0.0186611556090444, 
0.0176615698524595, 0.0157351197601704, 0.00773975706029995), 
    robust.se = c(0.0179877493992649, 0.0223101496182252, 0.0236349676291152, 
    0.0202282450263194, 0.0186724156095066, 0.00757289335696934
    ), statistic = c(-15.8486779212352, -41.0838100168282, -33.639898895892, 
    5.81553579528025, -30.5151427139716, 21.7018536605676), p.value = c(1.43569566146437e-56, 
    0, 4.38118085650704e-248, 6.04399775632908e-09, 1.64100047755937e-204, 
    1.97056906465211e-104))%>% 
  dplyr::mutate(lo_95ci= estimate- 1.96 * robust.se) %>% 
  dplyr::mutate(up_95ci= estimate+ 1.96 * robust.se) %>% 
  dplyr::mutate(HR= estimate) %>% 
  dplyr::mutate_at(c("HR","lo_95ci","up_95ci"),~exp(.))

invisible("Previous dropout protects posterior dropout, but 95% CI does not replicate that; 2024-04-16= protects from being observed and 95% cis too")
invisible("Moderate previous biopsychosocial status protects actual dropout")
invisible("Severe previous biopsychosocial status protects actual dropout")
invisible("0-90 days in previous tr. predicts actual dropout")
invisible("previous PSU protects actual dropout")

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

summary(i5$iiw.weight)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 0.7554  1.0000  1.0000  1.5860  1.0000  5.8803 
Code
 #   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 # 0.7543  1.0000  1.0000  1.5863  1.0000  5.8794 
 
ploti5<-
ggplot( data= as.data.frame(i5$iiw.weight),aes(x= `i5$iiw.weight`))+
    geom_histogram(bins=60)+
  theme_classic()+ xlim(0,10)
invisible("Reasonable, but still there are some bit extreme values")

#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:

i5b <- iiw.weights(
  Surv(time.lag,time,event)~ tr_outcome.lag+ comp_bpsc_y2_moderate.lag+ comp_bpsc_y3_severe.lag+ less_90d_tr1.lag+ policonsumo2.lag+ policonsumo2+
  cluster(id),
      id= "id",
      time= "time",
      event= "event",
      data= data_mine_miss_proc,
      invariant= "id",
      lagvars= c("time", "tr_outcome", "comp_bpsc_y2_moderate", "comp_bpsc_y3_severe", "less_90d_tr1","policonsumo2"),
      maxfu= maxfu_df,# 12*12,#Base_fiscalia_v15f_grant_23_24_long2$cens_time,
      lagfirst= c(1,1,0,1,1,1,1),
      first= T  #If TRUE, the first observation for each individual is assigned an intensity of 1. This is appropriate if the first visit is a baseline visit at which recruitment to the study occurred
      )

i5b$m
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    comp_bpsc_y2_moderate.lag + comp_bpsc_y3_severe.lag + less_90d_tr1.lag + 
    policonsumo2.lag + policonsumo2, data = datacox, cluster = id)

                              coef exp(coef) se(coef) robust se      z        p
tr_outcome.lag            0.171974  1.187647 0.018876  0.017987  9.561  < 2e-16
comp_bpsc_y2_moderate.lag 0.008456  1.008491 0.031855  0.029226  0.289    0.772
comp_bpsc_y3_severe.lag   0.597821  1.818153 0.030962  0.028960 20.643  < 2e-16
less_90d_tr1.lag          0.855059  2.351514 0.012903  0.014455 59.154  < 2e-16
policonsumo2.lag          0.123359  1.131291 0.019003  0.020580  5.994 2.04e-09
policonsumo2              0.062995  1.065021 0.007827  0.007610  8.277  < 2e-16

Likelihood ratio test=13404  on 6 df, p=< 2.2e-16
n= 87158, number of events= 87158 
   (75315 observations deleted due to missingness)
Code
broom::tidy(i5b$m)
# A tibble: 6 x 6
  term                      estimate std.error robust.se statistic  p.value
  <chr>                        <dbl>     <dbl>     <dbl>     <dbl>    <dbl>
1 tr_outcome.lag             0.172     0.0189    0.0180      9.56  1.17e-21
2 comp_bpsc_y2_moderate.lag  0.00846   0.0319    0.0292      0.289 7.72e- 1
3 comp_bpsc_y3_severe.lag    0.598     0.0310    0.0290     20.6   1.13e-94
4 less_90d_tr1.lag           0.855     0.0129    0.0145     59.2   0       
5 policonsumo2.lag           0.123     0.0190    0.0206      5.99  2.04e- 9
6 policonsumo2               0.0630    0.00783   0.00761     8.28  1.26e-16
Code
i5b_df_out<-
cbind.data.frame(term = c("tr_outcome.lag", "comp_bpsc_y2_moderate.lag", 
"comp_bpsc_y3_severe.lag", "less_90d_tr1.lag", "policonsumo2.lag", 
"policonsumo2"), estimate = c(0.171524496903936, 0.00483620529764353, 
0.596008396206245, 0.855542927694904, 0.125702439454889, 0.0627347379496959
), std.error = c(0.018874296525738, 0.0318585598048639, 0.0309630894506515, 
0.0128979749956164, 0.0189962053653323, 0.00782722711322035), 
    robust.se = c(0.0179913060388931, 0.0292985300929012, 0.0290119903127091, 
    0.0144451828760062, 0.020549409265887, 0.00759613906581795
    ), statistic = c(9.53374349439331, 0.165066482253842, 20.543519757938, 
    59.2268671873984, 6.11708287223425, 8.2587663819897), p.value = c(1.51711569984396e-21, 
    0.868891654364147, 8.79518334988573e-94, 0, 9.53038111046381e-10, 
    1.47185680747056e-16))%>% 
  dplyr::mutate(lo_95ci= estimate- 1.96 * robust.se) %>% 
  dplyr::mutate(up_95ci= estimate+ 1.96 * robust.se) %>% 
  dplyr::mutate(HR= estimate) %>% 
  dplyr::mutate_at(c("HR","lo_95ci","up_95ci"),~exp(.))

invisible("Previous dropout now predicts actual dropout")
invisible("Moderate previous biopsychosocial status predict actual dropout")
invisible("Severe previous biopsychosocial status predict actual dropout")
invisible("0-90 days in previous tr. predicts actual dropout")
invisible("previous PSU protects actual dropout")

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

summary(i5b$iiw.weight)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 0.1635  1.0000  1.0000  0.9053  1.0000  1.0000 
Code
#  Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
# 0.1634  1.0000  1.0000  0.9055  1.0000  1.0000 
 
invisible("Many low values")


ploti5b<-
ggplot( data= as.data.frame(i5b$iiw.weight),aes(x= `i5b$iiw.weight`))+
    geom_histogram(bins=60)+
  theme_classic()+ xlim(0,10)
invisible("Reasonable, but still there are some bit extreme values")
Code
iiw_coefs_df<-
dplyr::bind_rows(
  cbind.data.frame(w= "i0", i0_df_out),
  cbind.data.frame(w= "i0b", i0b_df_out),
  cbind.data.frame(w= "i0c", i0c_df_out),
  cbind.data.frame(w= "i0d", i0d_df_out),
  cbind.data.frame(w= "i1", i1_df_out),
  cbind.data.frame(w= "i1b", i1b_df_out),
  cbind.data.frame(w= "i1c", i1c_df_out),
  cbind.data.frame(w= "i1d", i1d_df_out),
  cbind.data.frame(w= "i2", i2_df_out),
  cbind.data.frame(w= "i3", i3_df_out),
  cbind.data.frame(w= "i3b", i3b_df_out),
  cbind.data.frame(w= "i4", i4_df_out),
  cbind.data.frame(w= "i4b", i4b_df_out),
  cbind.data.frame(w= "i4c", i4c_df_out),
  cbind.data.frame(w= "i4d", i4d_df_out),
  cbind.data.frame(w= "i5", i5_df_out),
  cbind.data.frame(w= "i5b", i5b_df_out)
) %>% dplyr::select(w, term, HR, lo_95ci, up_95ci, p.value)

iiw_coefs_df %>% 
   dplyr::mutate_at(c("HR", "lo_95ci", "up_95ci"), ~ sprintf("%1.2f",.)) %>% 
  dplyr::mutate_at(c("p.value"), ~ ifelse(.<.001,"<0.001",sprintf("%1.3f",.))) %>% 
  { 
    write.table(., file= paste0(getwd(),"/_proposal_grant/2023/iiws_coefs_24_04_18.csv"), dec=",", sep="\t")
    knitr::kable(., size=10, format="markdown",caption= "Different weights, coefficients", escape=T)
  }
Different weights, coefficients
w term HR lo_95ci up_95ci p.value
i0 tr_outcome.lag 0.15 0.15 0.16 <0.001
i0b tr_outcome.lag 2.91 2.82 3.01 <0.001
i0c tr_outcome.lag 0.47 0.47 0.48 <0.001
i0d tr_outcome.lag 1.32 1.28 1.35 <0.001
i1 tr_outcome.lag 1.20 1.15 1.24 <0.001
i1 comp_bpsc_y2_moderate.lag 1.17 1.10 1.24 <0.001
i1 comp_bpsc_y3_severe.lag 1.69 1.58 1.80 <0.001
i1 less_90d_tr1.lag 1.04 1.00 1.08 0.032
i1 policonsumo2.lag 1.22 1.17 1.27 <0.001
i1b tr_outcome.lag 1.23 1.18 1.28 <0.001
i1b comp_bpsc_y2_moderate.lag 1.00 0.94 1.07 0.910
i1b comp_bpsc_y3_severe.lag 4.21 3.95 4.48 <0.001
i1b less_90d_tr1.lag 3.59 3.51 3.68 <0.001
i1b policonsumo2.lag 2.32 2.22 2.42 <0.001
i1c tr_outcome.lag 0.74 0.72 0.76 <0.001
i1c comp_bpsc_y2_moderate.lag 0.40 0.39 0.42 <0.001
i1c comp_bpsc_y3_severe.lag 0.44 0.43 0.46 <0.001
i1c less_90d_tr1.lag 0.89 0.86 0.92 <0.001
i1c policonsumo2.lag 0.73 0.72 0.75 <0.001
i1d tr_outcome.lag 1.10 1.06 1.13 <0.001
i1d comp_bpsc_y2_moderate.lag 0.95 0.91 1.00 0.036
i1d comp_bpsc_y3_severe.lag 1.48 1.42 1.55 <0.001
i1d less_90d_tr1.lag 1.57 1.53 1.61 <0.001
i1d policonsumo2.lag 1.27 1.23 1.31 <0.001
i2 tr_outcome.lag 0.81 0.78 0.84 <0.001
i2 comp_bpsc_y2_moderate.lag 0.45 0.43 0.47 <0.001
i2 comp_bpsc_y3_severe.lag 0.52 0.50 0.55 <0.001
i2 less_90d_tr1.lag 1.18 1.13 1.23 <0.001
i2 policonsumo2.lag 0.61 0.59 0.63 <0.001
i2 treatment 0.77 0.75 0.79 <0.001
i3 tr_outcome.lag 0.75 0.73 0.78 <0.001
i3 comp_bpsc_y2_moderate.lag 0.40 0.38 0.42 <0.001
i3 comp_bpsc_y3_severe.lag 0.45 0.43 0.47 <0.001
i3 less_90d_tr1.lag 1.12 1.08 1.17 <0.001
i3 policonsumo2.lag 0.57 0.55 0.59 <0.001
i3 policonsumo2 1.18 1.16 1.20 <0.001
i3b tr_outcome.lag 1.19 1.15 1.23 <0.001
i3b comp_bpsc_y2_moderate.lag 1.00 0.95 1.06 0.869
i3b comp_bpsc_y3_severe.lag 1.81 1.71 1.92 <0.001
i3b less_90d_tr1.lag 2.35 2.29 2.42 <0.001
i3b policonsumo2.lag 1.13 1.09 1.18 <0.001
i3b policonsumo2 1.06 1.05 1.08 <0.001
i4 tr_outcome.lag 0.81 0.78 0.84 <0.001
i4 comp_bpsc_y2_moderate.lag 0.45 0.43 0.47 <0.001
i4 comp_bpsc_y3_severe.lag 0.52 0.50 0.55 <0.001
i4 less_90d_tr1.lag 1.18 1.13 1.23 <0.001
i4 policonsumo2.lag 0.61 0.59 0.63 <0.001
i4 treatment.lag 0.77 0.75 0.79 <0.001
i4b tr_outcome.lag 1.23 1.18 1.28 <0.001
i4b comp_bpsc_y2_moderate.lag 1.00 0.94 1.07 0.991
i4b comp_bpsc_y3_severe.lag 4.17 3.92 4.44 <0.001
i4b less_90d_tr1.lag 3.57 3.49 3.65 <0.001
i4b policonsumo2.lag 2.33 2.24 2.43 <0.001
i4b treatment.lag 1.16 1.12 1.19 <0.001
i4c tr_outcome.lag 1.03 1.00 1.06 0.076
i4c comp_bpsc_y2_moderate.lag 0.75 0.72 0.79 <0.001
i4c comp_bpsc_y3_severe.lag 0.88 0.84 0.92 <0.001
i4c less_90d_tr1.lag 1.13 1.09 1.17 <0.001
i4c policonsumo2.lag 0.83 0.81 0.86 <0.001
i4c treatment.lag 0.26 0.25 0.28 <0.001
i4d tr_outcome.lag 1.14 1.11 1.18 <0.001
i4d comp_bpsc_y2_moderate.lag 1.00 0.95 1.06 0.873
i4d comp_bpsc_y3_severe.lag 1.62 1.54 1.70 <0.001
i4d less_90d_tr1.lag 1.80 1.75 1.86 <0.001
i4d policonsumo2.lag 1.11 1.07 1.15 <0.001
i4d treatment.lag 0.26 0.24 0.28 <0.001
i5 tr_outcome.lag 0.75 0.73 0.78 <0.001
i5 comp_bpsc_y2_moderate.lag 0.40 0.38 0.42 <0.001
i5 comp_bpsc_y3_severe.lag 0.45 0.43 0.47 <0.001
i5 less_90d_tr1.lag 1.12 1.08 1.17 <0.001
i5 policonsumo2.lag 0.57 0.55 0.59 <0.001
i5 policonsumo2 1.18 1.16 1.20 <0.001
i5b tr_outcome.lag 1.19 1.15 1.23 <0.001
i5b comp_bpsc_y2_moderate.lag 1.00 0.95 1.06 0.869
i5b comp_bpsc_y3_severe.lag 1.81 1.71 1.92 <0.001
i5b less_90d_tr1.lag 2.35 2.29 2.42 <0.001
i5b policonsumo2.lag 1.13 1.09 1.18 <0.001
i5b policonsumo2 1.06 1.05 1.08 <0.001
Code
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

iiw_df<-
dplyr::bind_rows(
  cbind.data.frame(w= "i0", iiw=i0$iiw.weight),
  cbind.data.frame(w= "i0b", iiw=i0b$iiw.weight),
  cbind.data.frame(w= "i0c", iiw=i0c$iiw.weight),
  cbind.data.frame(w= "i0d", iiw=i0d$iiw.weight),
  cbind.data.frame(w= "i1", iiw=i1$iiw.weight),
  cbind.data.frame(w= "i1b", iiw=i1b$iiw.weight),
  cbind.data.frame(w= "i1c", iiw=i1c$iiw.weight),
  cbind.data.frame(w= "i1d", iiw=i1d$iiw.weight),
  cbind.data.frame(w= "i2", iiw=i2$iiw.weight),
  cbind.data.frame(w= "i3", iiw=i3$iiw.weight),
  cbind.data.frame(w= "i3b", iiw=i3b$iiw.weight),
  cbind.data.frame(w= "i4", iiw=i4$iiw.weight),
  cbind.data.frame(w= "i4b", iiw=i4b$iiw.weight),
  cbind.data.frame(w= "i4c", iiw=i4c$iiw.weight),
  cbind.data.frame(w= "i4d", iiw=i4d$iiw.weight),
  cbind.data.frame(w= "i5", iiw=i5$iiw.weight),
  cbind.data.frame(w= "i5b", iiw=i5b$iiw.weight)
)

psych::describeBy(iiw~w, data=iiw_df, mat=T) %>% 
   { 
     write.table(., file = paste0(getwd(),"/_proposal_grant/2023/iiws_24_04_18.csv"), dec=",", sep="\t")
     knitr::kable(., size=10, format="markdown",caption= "Different weights, summary descriptives", escape=T)
   }
Different weights, summary descriptives
item group1 vars n mean sd median trimmed mad min max range skew kurtosis se
iiw1 1 i0 1 90075 1.8464076 1.9802604 1.0000000 1.3731192 0.0000000 1.0000000 6.479385 5.479385e+00 1.9121364 1.6562841 0.0065981
iiw2 2 i0b 1 90075 0.8986002 0.2372356 1.0000000 0.9553002 0.0000000 0.3435686 1.000000 6.564314e-01 -1.9121364 1.6562841 0.0007905
iiw3 3 i0c 1 30988 1.4988009 0.5526275 1.0000000 1.4846252 0.0000000 1.0000000 2.111044 1.111044e+00 0.2052710 -1.9579270 0.0031393
iiw4 4 i0d 1 30988 0.7889996 0.0784465 0.7598355 0.7662774 0.0000000 0.7598355 1.000000 2.401645e-01 2.3178907 3.3727262 0.0004456
iiw5 5 i1 1 90075 0.9119083 0.1872022 1.0000000 0.9587877 0.0000000 0.3903843 1.000000 6.096157e-01 -1.8254047 1.6834181 0.0006237
iiw6 6 i1b 1 90075 0.8542926 0.3133476 1.0000000 0.9333340 0.0000000 0.0232989 1.000000 9.767011e-01 -1.8044064 1.4691605 0.0010441
iiw7 7 i1c 1 30988 2.6599175 1.5977913 3.0455687 2.5897254 2.3898431 1.0000000 5.104169 4.104169e+00 0.1254937 -1.7159442 0.0090766
iiw8 8 i1d 1 30988 0.4845744 0.2113451 0.3917356 0.4541405 0.1221457 0.3093494 1.047275 7.379257e-01 0.8898299 -0.4759514 0.0012006
iiw9 9 i2 1 90075 1.7202764 1.6227108 1.0000000 1.3104177 0.0000000 1.0000000 44.918862 4.391886e+01 2.6481313 13.3027889 0.0054068
iiw10 10 i3 1 90075 1.5860218 1.2752966 1.0000000 1.2569181 0.0000000 0.7554467 5.880294 5.124847e+00 1.9150319 2.0573721 0.0042492
iiw11 11 i3b 1 90075 0.9053333 0.2172963 1.0000000 0.9668945 0.0000000 0.1634565 1.000000 8.365435e-01 -2.2189636 3.5708295 0.0007240
iiw12 12 i4 1 90075 1.7202764 1.6227108 1.0000000 1.3104177 0.0000000 1.0000000 44.918862 4.391886e+01 2.6481313 13.3027889 0.0054068
iiw13 13 i4b 1 90075 0.8535921 0.3146328 1.0000000 0.9326223 0.0000000 0.0086746 1.023063 1.014388e+00 -1.7995183 1.4485057 0.0010483
iiw14 14 i4c 1 30988 4.9086872 289.9031455 0.8565799 0.7783143 0.9844717 0.1925629 49523.911445 4.952372e+04 162.2555130 27512.6578355 1.6468580
iiw15 15 i4d 1 30988 1.5153260 104.0729164 0.2103919 0.2517302 0.1181386 0.1307086 18136.282466 1.813615e+04 170.9329232 29750.5713178 0.5912089
iiw16 16 i5 1 90075 1.5860218 1.2752966 1.0000000 1.2569181 0.0000000 0.7554467 5.880294 5.124847e+00 1.9150319 2.0573721 0.0042492
iiw17 17 i5b 1 90075 0.9053333 0.2172963 1.0000000 0.9668945 0.0000000 0.1634565 1.000000 8.365435e-01 -2.2189636 3.5708295 0.0007240
Code
library(cowplot)

Attaching package: ‘cowplot’

The following object is masked from ‘package:ggpubr’:

get_legend
Code
combined_plot <- plot_grid(ploti0, ploti0b, ploti0c, ploti0d, ploti1, ploti1b, ploti1c, ploti1d, ploti2, ploti3, ploti3b, ploti4, ploti4b, ploti4c, ploti4d, ploti5, ploti5b, ncol = 1, align = 'v', axis = 'tb')

print(combined_plot)

Code
ggsave(combined_plot, file="_proposal_grant/2023/_figs/comb_plot_24_04_18.jpg", width=8.5, height=15.5, dpi=500)
Code
folder_path <- ifelse(dir.exists("E:/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/"),
                      "E:/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/",
                      "C:/Users/CISS Fondecyt/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/")
save.image(paste0(folder_path,"20240430_avance_iiw.RData"))

We included several covariates in the model, such as PSU (policonsumo2), previous treatment completion status (if any, event.lag), previous biopsychosocial status (if any, lag(compromiso_biopsicosocial.y)), sex (sex), age of onset of substance use (edad_ini_cons), educational attainment (escolaridad_rec), primary substance at admission to treatment (sus_principal_mod), frequency of primary substance (freq_cons_sus_prin_ord), occupational status (condicion_ocupacional_corr24), number of children (binary, num_hijos_mod_joel_bin), housing tenure status (tenencia_de_la_vivienda_mod), administrative macro-zone of municipality of residence (macrozona), psychiatric co-morbidity (ICD-10, dg_cie_10_rec), SUD severity diagnosis (ICD-10, dg_trs_cons_sus_or), rurality status of municipality of residence (clas_r), poverty index of municipality of residence (porc_pobr), starting substance used (sus_ini_mod_mvv), corrected year of birth (ano_nac_corr), and cohabitation status (who the individual lives with, con_quien_vive_joel). These covariates are used to adjust for potential confounding factors that may affect the survival outcome.

We provisionally selected i4d (not only events; dichotomous transformation of previous biopsychosocial compromise, previous time in treatment (0-90 vs. >90), previous PSU and treatment counts). However, Lokku et al., does not recommend including treatment counts. So we ended selecting i1 : Not only events; dichotomous transformation of previous biopsychosocial compromise, previous time in treatment (0-90 vs. >90), previous PSU and lag first 1s; without treatment count.


Definitive model

To account for the dynamic relationship between time since follow-up, we included terms for time elapsed from the first treatment.

Code
invisible("2024-04-17: data_mine2_miss_proc2 not needed, data_mine_miss_proc2 instead (no longer needed to select events==1 [only with visits], because we already selected them)")

data_mine_miss_proc2 <-
dplyr::mutate(group_by(data_mine_miss_proc, hash_key), lag_tr_outcome= lag(tr_outcome, default=1), lag_comp_bpsc_y2_moderate= lag(comp_bpsc_y2_moderate, default=0), lag_comp_bpsc_y3_severe= lag(comp_bpsc_y3_severe, default=1), lag_fech_egres_num= lag(fech_egres_num), lag_less_90d_tr1= lag(less_90d_tr1, default=1), lag_policonsumo2= lag(policonsumo2, default=1), lag_dias_treat_imp_sin_na= lag(dias_treat_imp_sin_na)) %>% dplyr::ungroup()%>% 
    dplyr::mutate(log_lag_dias_treat_imp_sin_na= log(lag_dias_treat_imp_sin_na+0.001), log_dias_treat_imp_sin_na= log(dias_treat_imp_sin_na+0.001), esc_dum_rec_3prim = ifelse(as.character(escolaridad_rec)=="3-Completed primary school or less",1,0), esc_dum_rec_2high = ifelse(as.character(escolaridad_rec)== "2-Completed high school or less", 1, 0), susprindum_oh = ifelse(as.character(sus_principal_mod)== "Alcohol", 1, 0), susprindum_coc = ifelse(as.character(sus_principal_mod)== "Cocaine hydrochloride", 1, 0), susprindum_pbc = ifelse(as.character(sus_principal_mod)== "Cocaine paste", 1, 0), susprindum_mar = ifelse(as.character(sus_principal_mod)== "Marijuana", 1, 0), freq_cons_dum_5day= ifelse(as.character(freq_cons_sus_prin_ord)== "5. Daily", 1, 0), freq_cons_dum_44to6wk= ifelse(as.character(freq_cons_sus_prin_ord)== "4. 4 to 6 days a week", 1, 0), freq_cons_dum_32to3wk= ifelse(as.character(freq_cons_sus_prin_ord)== "3. 2 to 3 days a week", 1, 0), freq_cons_dum_21wkmore= ifelse(as.character(freq_cons_sus_prin_ord)== "2. 1 day a week or more", 1, 0), cond_oc_dum_3unemp= ifelse(as.character(condicion_ocupacional_corr24)== "Unemployed", 1, 0), cond_oc_dum_2inact= ifelse(as.character(condicion_ocupacional_corr24)== "Inactive", 1, 0), viv_dum_illegal= ifelse(as.character(tenencia_de_la_vivienda_mod)== "Illegal Settlement", 1, 0), viv_dum_own= ifelse(as.character(tenencia_de_la_vivienda_mod)== "Owner/Transferred dwellings/Pays Dividends", 1, 0), viv_dum_rent= ifelse(as.character(tenencia_de_la_vivienda_mod)== "Renting", 1, 0), viv_dum_temp= ifelse(as.character(tenencia_de_la_vivienda_mod)== "Stays temporarily with a relative", 1, 0), macro_dum_south= ifelse(as.character(macrozona)== "South", 1, 0), macro_dum_north= ifelse(as.character(macrozona)== "North", 1, 0), psycom_dum_with= ifelse(as.character(dg_cie_10_rec)== "With psychiatric comorbidity", 1, 0), psycom_dum_study= ifelse(as.character(dg_cie_10_rec)== "Diagnosis unknown (under study)", 1, 0), psycom_dum_study= ifelse(as.character(dg_cie_10_rec)== "Diagnosis unknown (under study)", 1, 0), rurality_rural= ifelse(as.character(clas_r)== "Rural", 1, 0), rurality_mix= ifelse(as.character(clas_r)== "Mixed", 1, 0), susinidum_oh = ifelse(as.character(sus_ini_mod_mvv)== "Alcohol", 1, 0), susinidum_coc = ifelse(as.character(sus_ini_mod_mvv)== "Cocaine hydrochloride", 1, 0), susinidum_pbc = ifelse(as.character(sus_ini_mod_mvv)== "Cocaine paste", 1, 0), susinidum_mar = ifelse(as.character(sus_ini_mod_mvv)== "Marijuana", 1, 0), cohab_dum_alone = ifelse(as.character(con_quien_vive_joel)== "Alone", 1, 0), cohab_dum_fam_or = ifelse(as.character(con_quien_vive_joel)== "Family of origin", 1, 0), cohab_dum_cpl_child = ifelse(as.character(con_quien_vive_joel)== "With couple/children", 1, 0), surv_time= ifelse(surv_time<0.001, 0.0001, surv_time)) %>% data.frame() 


invisible("2024-05-02: problem with different values in people with plans (possibly imputed)")

data_mine_miss_restr_proc2 <-
dplyr::mutate(group_by(data_mine_miss_restr_proc, hash_key), lag_tr_outcome= lag(tr_outcome, default=1), lag_comp_bpsc_y2_moderate= lag(comp_bpsc_y2_moderate, default=0), lag_comp_bpsc_y3_severe= lag(comp_bpsc_y3_severe, default=1), lag_fech_egres_num= lag(fech_egres_num), lag_less_90d_tr1= lag(less_90d_tr1, default=1), lag_policonsumo2= lag(policonsumo2, default=1), lag_dias_treat_imp_sin_na= lag(dias_treat_imp_sin_na)) %>% dplyr::ungroup()%>% 
    dplyr::mutate(log_lag_dias_treat_imp_sin_na= log(lag_dias_treat_imp_sin_na+0.001), log_dias_treat_imp_sin_na= log(dias_treat_imp_sin_na+0.001), esc_dum_rec_3prim = ifelse(as.character(escolaridad_rec)=="3-Completed primary school or less",1,0), esc_dum_rec_2high = ifelse(as.character(escolaridad_rec)== "2-Completed high school or less", 1, 0), susprindum_oh = ifelse(as.character(sus_principal_mod)== "Alcohol", 1, 0), susprindum_coc = ifelse(as.character(sus_principal_mod)== "Cocaine hydrochloride", 1, 0), susprindum_pbc = ifelse(as.character(sus_principal_mod)== "Cocaine paste", 1, 0), susprindum_mar = ifelse(as.character(sus_principal_mod)== "Marijuana", 1, 0), freq_cons_dum_5day= ifelse(as.character(freq_cons_sus_prin_ord)== "5. Daily", 1, 0), freq_cons_dum_44to6wk= ifelse(as.character(freq_cons_sus_prin_ord)== "4. 4 to 6 days a week", 1, 0), freq_cons_dum_32to3wk= ifelse(as.character(freq_cons_sus_prin_ord)== "3. 2 to 3 days a week", 1, 0), freq_cons_dum_21wkmore= ifelse(as.character(freq_cons_sus_prin_ord)== "2. 1 day a week or more", 1, 0), cond_oc_dum_3unemp= ifelse(as.character(condicion_ocupacional_corr24)== "Unemployed", 1, 0), cond_oc_dum_2inact= ifelse(as.character(condicion_ocupacional_corr24)== "Inactive", 1, 0), viv_dum_illegal= ifelse(as.character(tenencia_de_la_vivienda_mod)== "Illegal Settlement", 1, 0), viv_dum_own= ifelse(as.character(tenencia_de_la_vivienda_mod)== "Owner/Transferred dwellings/Pays Dividends", 1, 0), viv_dum_rent= ifelse(as.character(tenencia_de_la_vivienda_mod)== "Renting", 1, 0), viv_dum_temp= ifelse(as.character(tenencia_de_la_vivienda_mod)== "Stays temporarily with a relative", 1, 0), macro_dum_south= ifelse(as.character(macrozona)== "South", 1, 0), macro_dum_north= ifelse(as.character(macrozona)== "North", 1, 0), psycom_dum_with= ifelse(as.character(dg_cie_10_rec)== "With psychiatric comorbidity", 1, 0), psycom_dum_study= ifelse(as.character(dg_cie_10_rec)== "Diagnosis unknown (under study)", 1, 0), psycom_dum_study= ifelse(as.character(dg_cie_10_rec)== "Diagnosis unknown (under study)", 1, 0), rurality_rural= ifelse(as.character(clas_r)== "Rural", 1, 0), rurality_mix= ifelse(as.character(clas_r)== "Mixed", 1, 0), susinidum_oh = ifelse(as.character(sus_ini_mod_mvv)== "Alcohol", 1, 0), susinidum_coc = ifelse(as.character(sus_ini_mod_mvv)== "Cocaine hydrochloride", 1, 0), susinidum_pbc = ifelse(as.character(sus_ini_mod_mvv)== "Cocaine paste", 1, 0), susinidum_mar = ifelse(as.character(sus_ini_mod_mvv)== "Marijuana", 1, 0), cohab_dum_alone = ifelse(as.character(con_quien_vive_joel)== "Alone", 1, 0), cohab_dum_fam_or = ifelse(as.character(con_quien_vive_joel)== "Family of origin", 1, 0), cohab_dum_cpl_child = ifelse(as.character(con_quien_vive_joel)== "With couple/children", 1, 0), surv_time= ifelse(surv_time<0.001, 0.0001, surv_time)) %>% 
 dplyr::group_by(id) %>% 
 dplyr::mutate(tipo_de_plan_2_mod=first(tipo_de_plan_2_mod)) %>% 
 dplyr::ungroup() %>% 
data.frame() 


dis_run_w_more_type_plan<-
data_mine_miss_restr_proc2 %>% 
    dplyr::group_by(id) %>% 
    dplyr::summarise(count_n_dis=n_distinct(tipo_de_plan_2_mod)) %>% 
    dplyr::filter(count_n_dis>1) %>% distinct(id)
# data_mine_miss_restr_proc2 %>% 
# dplyr::group_by(id) %>% 
# dplyr::mutate(tipo_de_plan_2_mod=first(tipo_de_plan_2_mod)) %>% 
# dplyr::ungroup() %>% 
# dplyr::filter(id  %in% dis_run_w_more_type_plan$id)

invisible("export to python")
arrow::write_parquet(data_mine_miss_proc2 %>% dplyr::mutate(lag_time=ifelse(is.na(lag_time),0,lag_time)), 
                     "_proposal_grant/2023/data_mine_miss_proc2_20240417.gz.parquet", 
                     compression = "gzip", compression_level = 5)
arrow::write_parquet(data_mine_miss_restr_proc2 %>% dplyr::mutate(lag_time=ifelse(is.na(lag_time),0,lag_time)), 
                     "_proposal_grant/2023/data_mine_miss_restr_proc2_20240427.gz.parquet", 
                     compression = "gzip", compression_level = 5)

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
invisible("Explore residuals")
coxph(Surv(lag_time,time,event)~ lag_tr_outcome+ lag_comp_bpsc_y3_severe+ lag_less_90d_tr1+ log_lag_dias_treat_imp_sin_na+ lag_policonsumo2+ edad_al_ing_1+ ano_nac_corr+ cluster(id)+ strata(tipo_de_plan_2), data = data_mine_miss_proc2)
Call:
coxph(formula = Surv(lag_time, time, event) ~ lag_tr_outcome + 
    lag_comp_bpsc_y3_severe + lag_less_90d_tr1 + log_lag_dias_treat_imp_sin_na + 
    lag_policonsumo2 + edad_al_ing_1 + ano_nac_corr + strata(tipo_de_plan_2), 
    data = data_mine_miss_proc2, cluster = id)

                                   coef exp(coef)  se(coef) robust se      z
lag_tr_outcome                 0.139006  1.149130  0.019532  0.017338  8.017
lag_comp_bpsc_y3_severe        0.128602  1.137237  0.015722  0.014992  8.578
lag_less_90d_tr1               0.119770  1.127237  0.021018  0.023973  4.996
log_lag_dias_treat_imp_sin_na -0.027843  0.972541  0.007598  0.010508 -2.650
lag_policonsumo2              -0.011019  0.989041  0.020273  0.018189 -0.606
edad_al_ing_1                  0.232879  1.262229  0.004098  0.003826 60.866
ano_nac_corr                   0.236261  1.266505  0.004070  0.003788 62.373
                                     p
lag_tr_outcome                1.08e-15
lag_comp_bpsc_y3_severe        < 2e-16
lag_less_90d_tr1              5.85e-07
log_lag_dias_treat_imp_sin_na  0.00805
lag_policonsumo2               0.54462
edad_al_ing_1                  < 2e-16
ano_nac_corr                   < 2e-16

Likelihood ratio test=3814  on 7 df, p=< 2.2e-16
n= 17671, number of events= 17671 
   (72404 observations deleted due to missingness)
Code
#                                    coef exp(coef)  se(coef) robust se      z        p
# lag_tr_outcome                 0.139016  1.149142  0.019531  0.017338  8.018 1.07e-15
# lag_comp_bpsc_y3_severe        0.129501  1.138261  0.015740  0.015006  8.630  < 2e-16
# lag_less_90d_tr1               0.119756  1.127222  0.021016  0.023976  4.995 5.89e-07
# log_lag_dias_treat_imp_sin_na -0.027876  0.972509  0.007595  0.010510 -2.652    0.008
# lag_policonsumo2              -0.010438  0.989616  0.020266  0.018185 -0.574    0.566
# edad_al_ing_1                  0.232959  1.262330  0.004099  0.003827 60.870  < 2e-16
# ano_nac_corr                   0.236313  1.266571  0.004070  0.003788 62.377  < 2e-16


coxph(Surv(lag_time,time,event)~ lag_tr_outcome+ lag_comp_bpsc_y3_severe+ lag_less_90d_tr1+ log_lag_dias_treat_imp_sin_na+ lag_policonsumo2+ edad_al_ing_1+ ano_nac_corr+ cluster(id)+ strata(tipo_de_plan_2), data = data_mine_miss_restr_proc2)
Call:
coxph(formula = Surv(lag_time, time, event) ~ lag_tr_outcome + 
    lag_comp_bpsc_y3_severe + lag_less_90d_tr1 + log_lag_dias_treat_imp_sin_na + 
    lag_policonsumo2 + edad_al_ing_1 + ano_nac_corr + strata(tipo_de_plan_2), 
    data = data_mine_miss_restr_proc2, cluster = id)

                                   coef exp(coef)  se(coef) robust se      z
lag_tr_outcome                 0.139006  1.149130  0.019532  0.017338  8.017
lag_comp_bpsc_y3_severe        0.128602  1.137237  0.015722  0.014992  8.578
lag_less_90d_tr1               0.119770  1.127237  0.021018  0.023973  4.996
log_lag_dias_treat_imp_sin_na -0.027843  0.972541  0.007598  0.010508 -2.650
lag_policonsumo2              -0.011019  0.989041  0.020273  0.018189 -0.606
edad_al_ing_1                  0.232879  1.262229  0.004098  0.003826 60.866
ano_nac_corr                   0.236261  1.266505  0.004070  0.003788 62.373
                                     p
lag_tr_outcome                1.08e-15
lag_comp_bpsc_y3_severe        < 2e-16
lag_less_90d_tr1              5.85e-07
log_lag_dias_treat_imp_sin_na  0.00805
lag_policonsumo2               0.54462
edad_al_ing_1                  < 2e-16
ano_nac_corr                   < 2e-16

Likelihood ratio test=3814  on 7 df, p=< 2.2e-16
n= 17671, number of events= 17671 
   (13317 observations deleted due to missingness)
Code
#                                 coef exp(coef) se(coef) robust se    z      p
# lag_tr_outcome                 0.139     1.149    0.020     0.017  8.0  1e-15
# lag_comp_bpsc_y3_severe        0.128     1.137    0.016     0.015  8.6 <2e-16
# lag_less_90d_tr1               0.120     1.128    0.021     0.024  5.0  5e-07
# log_lag_dias_treat_imp_sin_na -0.028     0.972    0.008     0.011 -2.7  0.008
# lag_policonsumo2              -0.010     0.990    0.020     0.018 -0.6  0.574
# edad_al_ing_1                  0.233     1.262    0.004     0.004 60.9 <2e-16
# ano_nac_corr                   0.236     1.267    0.004     0.004 62.4 <2e-16

cox.zph(
  coxph(Surv(lag_time,time,event)~ lag_tr_outcome+ lag_comp_bpsc_y3_severe+ lag_less_90d_tr1+ log_lag_dias_treat_imp_sin_na+ lag_policonsumo2+ edad_al_ing_1+ ano_nac_corr+ cluster(id)+ strata(tipo_de_plan_2), data= data_mine_miss_proc2)
)
                               chisq df       p
lag_tr_outcome                 37.88  1 7.5e-10
lag_comp_bpsc_y3_severe        61.07  1 5.5e-15
lag_less_90d_tr1              147.34  1 < 2e-16
log_lag_dias_treat_imp_sin_na 178.31  1 < 2e-16
lag_policonsumo2               13.27  1 0.00027
edad_al_ing_1                  35.36  1 2.7e-09
ano_nac_corr                    8.64  1 0.00328
GLOBAL                        521.18  7 < 2e-16
Code
#                                chisq df       p
# lag_tr_outcome                 37.98  1 7.1e-10
# lag_comp_bpsc_y3_severe        61.87  1 3.7e-15
# lag_less_90d_tr1              147.28  1 < 2e-16
# log_lag_dias_treat_imp_sin_na 178.13  1 < 2e-16
# lag_policonsumo2               13.18  1 0.00028
# edad_al_ing_1                  35.57  1 2.5e-09
# ano_nac_corr                    8.73  1 0.00312
# GLOBAL                        521.92  7 < 2e-16


cox.zph(
  coxph(Surv(lag_time,time,event)~ lag_tr_outcome+ lag_comp_bpsc_y3_severe+ lag_less_90d_tr1+ log_lag_dias_treat_imp_sin_na+ lag_policonsumo2+ edad_al_ing_1+ ano_nac_corr+ cluster(id)+ strata(tipo_de_plan_2), data= data_mine_miss_restr_proc2)
)
                               chisq df       p
lag_tr_outcome                 37.88  1 7.5e-10
lag_comp_bpsc_y3_severe        61.07  1 5.5e-15
lag_less_90d_tr1              147.34  1 < 2e-16
log_lag_dias_treat_imp_sin_na 178.31  1 < 2e-16
lag_policonsumo2               13.27  1 0.00027
edad_al_ing_1                  35.36  1 2.7e-09
ano_nac_corr                    8.64  1 0.00328
GLOBAL                        521.18  7 < 2e-16
Code
#                                chisq df       p
# lag_tr_outcome                 37.95  1 7.3e-10
# lag_comp_bpsc_y3_severe        57.93  1 2.7e-14
# lag_less_90d_tr1              147.25  1 < 2e-16
# log_lag_dias_treat_imp_sin_na 178.15  1 < 2e-16
# lag_policonsumo2               13.21  1 0.00028
# edad_al_ing_1                  35.50  1 2.6e-09
# ano_nac_corr                    8.71  1 0.00317
# GLOBAL                        519.01  7 < 2e-16

invisible("Tiene mejor ajuste que las alternativas sólo con días de tto. continuos (log) o el dicotomizado solo:  186751.2 vs. 186756.6 vs. 186779.1; 2024-04-21: 261769.5 el actual sin moderado 262293.3, vs. mucho menos cuando intento agregar términos. Pero lo haré dirigido por la teoría")

cox.zph(
  coxph(Surv(lag_time,time,event)~ lag_tr_outcome+ lag_comp_bpsc_y3_severe+ lag_less_90d_tr1+ log_lag_dias_treat_imp_sin_na+ lag_policonsumo2+ edad_al_ing_1+ ano_nac_corr+ cluster(id)+ strata(tipo_de_plan_2_mod), data= data_mine_miss_restr_proc2)
)
                               chisq df       p
lag_tr_outcome                 54.28  1 1.7e-13
lag_comp_bpsc_y3_severe        20.29  1 6.7e-06
lag_less_90d_tr1              130.84  1 < 2e-16
log_lag_dias_treat_imp_sin_na 142.09  1 < 2e-16
lag_policonsumo2                9.78  1  0.0018
edad_al_ing_1                  33.55  1 7.0e-09
ano_nac_corr                    7.91  1  0.0049
GLOBAL                        460.60  7 < 2e-16
Code
#                                chisq df       p
# lag_tr_outcome                 54.66  1 1.4e-13
# lag_comp_bpsc_y3_severe        19.67  1 9.2e-06
# lag_less_90d_tr1              130.48  1 < 2e-16
# log_lag_dias_treat_imp_sin_na 141.52  1 < 2e-16
# lag_policonsumo2                9.72  1  0.0018
# edad_al_ing_1                  33.47  1 7.2e-09
# ano_nac_corr                    7.88  1  0.0050
# GLOBAL                        459.34  7 < 2e-16

final <- iiw.weights(
   Surv(time.lag,time,event)~ tr_outcome.lag+ comp_bpsc_y3_severe.lag+ less_90d_tr1.lag+ log_dias_treat_imp_sin_na.lag+ policonsumo2.lag+ edad_al_ing_1+ ano_nac_corr+ 
        cluster(hash_key),
    id= "id",
    time= "time",
    event= "event",
    data= data_mine_miss_proc2,
    invariant= c("id", "edad_al_ing_1", "ano_nac_corr"),
    lagvars= c("time", "tr_outcome", "comp_bpsc_y3_severe", "less_90d_tr1", "log_dias_treat_imp_sin_na","policonsumo2"),
    maxfu= maxfu_df,#Base_fiscalia_v15f_grant_23_24_long2$cens_time,
    lagfirst= c(2.95082,1,1,1,4.499811,1), #90/30.5 4.499811 es 90 días
    first= T  #If TRUE, the first observation for each individual is assigned an intensity of 1. This is appropriate if the first visit is a baseline visit at which recruitment to the study occurred
)

final_restr <- iiw.weights(
   Surv(time.lag,time,event)~ tr_outcome.lag+ comp_bpsc_y3_severe.lag+ less_90d_tr1.lag+ log_dias_treat_imp_sin_na.lag+ policonsumo2.lag+ edad_al_ing_1+ ano_nac_corr+ 
        cluster(hash_key),
    id= "id",
    time= "time",
    event= "event",
    data= data_mine_miss_restr_proc2,
    invariant= c("id", "edad_al_ing_1", "ano_nac_corr"),
    lagvars= c("time", "tr_outcome", "comp_bpsc_y3_severe", "less_90d_tr1", "log_dias_treat_imp_sin_na","policonsumo2"),
    maxfu= maxfu_restr_df,#Base_fiscalia_v15f_grant_23_24_long2$cens_time,
    lagfirst= c(2.95082,1,1,1,4.499811,1), #90/30.5 4.499811 es 90 días
    first= T  #If TRUE, the first observation for each individual is assigned an intensity of 1. This is appropriate if the first visit is a baseline visit at which recruitment to the study occurred
)

final_b <- iiw.weights(
   Surv(time.lag,time,event)~ tr_outcome.lag+ comp_bpsc_y3_severe.lag+ less_90d_tr1.lag+ log_dias_treat_imp_sin_na.lag+ policonsumo2.lag+ edad_al_ing_1+ ano_nac_corr+ 
        cluster(hash_key),
    id= "id",
    time= "time",
    event= "event",
    data= data_mine_miss_proc2,
    invariant= c("id", "edad_al_ing_1", "ano_nac_corr"),
    lagvars= c("time", "tr_outcome", "comp_bpsc_y3_severe", "less_90d_tr1", "log_dias_treat_imp_sin_na","policonsumo2"),
    maxfu= maxfu_df,#Base_fiscalia_v15f_grant_23_24_long2$cens_time,
    lagfirst= c(2.95082,0,0,0,4.499811/2,0), #90/30.5 4.499811 es 90 días
    first= T  #If TRUE, the first observation for each individual is assigned an intensity of 1. This is appropriate if the first visit is a baseline visit at which recruitment to the study occurred
)

final_restr_b <- iiw.weights(
   Surv(time.lag,time,event)~ tr_outcome.lag+ comp_bpsc_y3_severe.lag+ less_90d_tr1.lag+ log_dias_treat_imp_sin_na.lag+ policonsumo2.lag+ edad_al_ing_1+ ano_nac_corr+ 
        cluster(hash_key),
    id= "id",
    time= "time",
    event= "event",
    data= data_mine_miss_restr_proc2,
    invariant= c("id", "edad_al_ing_1", "ano_nac_corr"),
    lagvars= c("time", "tr_outcome", "comp_bpsc_y3_severe", "less_90d_tr1", "log_dias_treat_imp_sin_na","policonsumo2"),
    maxfu= maxfu_restr_df,#Base_fiscalia_v15f_grant_23_24_long2$cens_time,
    lagfirst= c(2.95082,0,0,0,4.499811/2,0), #90/30.5 4.499811 es 90 días
    first= T  #If TRUE, the first observation for each individual is assigned an intensity of 1. This is appropriate if the first visit is a baseline visit at which recruitment to the study occurred
)
# 
broom::tidy(final$m, conf.int=T, exponentiate =T)
# A tibble: 7 x 8
  term                 estim~1 std.e~2 robus~3 stati~4   p.value conf.~5 conf.~6
  <chr>                  <dbl>   <dbl>   <dbl>   <dbl>     <dbl>   <dbl>   <dbl>
1 tr_outcome.lag          1.33 0.0194  0.0183    15.6  4.62e- 55    1.28    1.38
2 comp_bpsc_y3_severe~    1.72 0.0129  0.0135    40.3  0            1.68    1.77
3 less_90d_tr1.lag        3.30 0.0179  0.0205    58.3  0            3.17    3.44
4 log_dias_treat_imp_~    1.39 0.0106  0.0139    23.9  2.11e-126    1.36    1.43
5 policonsumo2.lag        1.10 0.0191  0.0196     5.07 4.03e-  7    1.06    1.15
6 edad_al_ing_1           1.08 0.00153 0.00141   57.9  0            1.08    1.09
7 ano_nac_corr            1.09 0.00153 0.00140   64.4  0            1.09    1.10
# ... with abbreviated variable names 1: estimate, 2: std.error, 3: robust.se,
#   4: statistic, 5: conf.low, 6: conf.high
Code
#   term                    estimate std.error robust.se statistic  p.value conf.low conf.high
#   <chr>                      <dbl>     <dbl>     <dbl>     <dbl>    <dbl>    <dbl>     <dbl>
# 1 tr_outcome.lag              1.17   0.0189    0.0172       9.01 2.07e-19     1.13      1.21
# 2 comp_bpsc_y3_severe.lag     1.80   0.0126    0.0130      45.3  0            1.75      1.85
# 3 less_90d_tr1.lag            2.27   0.0130    0.0142      57.6  0            2.21      2.34
# 4 policonsumo2.lag            1.18   0.0189    0.0196       8.30 1.06e-16     1.13      1.22
# 5 edad_al_ing_1               1.09   0.00153   0.00139     60.3  0            1.08      1.09
# 6 ano_nac_corr                1.10   0.00153   0.00138     66.2  0            1.09      1.10

broom::tidy(final_restr$m, conf.int=T, exponentiate =T)
# A tibble: 7 x 8
  term                 estim~1 std.e~2 robus~3 stati~4   p.value conf.~5 conf.~6
  <chr>                  <dbl>   <dbl>   <dbl>   <dbl>     <dbl>   <dbl>   <dbl>
1 tr_outcome.lag          1.27 0.0196  0.0175    13.7  5.54e- 43    1.23    1.32
2 comp_bpsc_y3_severe~    1.58 0.0135  0.0130    35.0  4.07e-268    1.54    1.62
3 less_90d_tr1.lag        2.47 0.0198  0.0221    40.9  0            2.37    2.58
4 log_dias_treat_imp_~    1.26 0.0109  0.0132    17.6  1.17e- 69    1.23    1.29
5 policonsumo2.lag        1.12 0.0195  0.0179     6.17 6.73e- 10    1.08    1.16
6 edad_al_ing_1           1.15 0.00296 0.00266   53.2  0            1.15    1.16
7 ano_nac_corr            1.15 0.00293 0.00263   54.4  0            1.15    1.16
# ... with abbreviated variable names 1: estimate, 2: std.error, 3: robust.se,
#   4: statistic, 5: conf.low, 6: conf.high
Code
# term                          estimate std.error robust.se statistic   p.value conf.low conf.high
# <chr>                            <dbl>     <dbl>     <dbl>     <dbl>     <dbl>    <dbl>     <dbl>
# tr_outcome.lag                    1.27   0.0196    0.0175      13.7  9.41e- 43     1.23      1.32
# comp_bpsc_y3_severe.lag           1.58   0.0135    0.0130      35.1  5.67e-270     1.54      1.62
# less_90d_tr1.lag                  2.47   0.0198    0.0221      41.0  0             2.37      2.58
# log_dias_treat_imp_sin_na.lag     1.26   0.0109    0.0132      17.6  1.97e- 69     1.23      1.29
# policonsumo2.lag                  1.12   0.0195    0.0179       6.31 2.70e- 10     1.08      1.16
# edad_al_ing_1                     1.15   0.00296   0.00267     53.2  0             1.15      1.16
# ano_nac_corr                      1.15   0.00293   0.00263     54.4  0             1.15      1.16

broom::tidy(final_b$m, conf.int=T, exponentiate =T)
# A tibble: 7 x 8
  term                 estim~1 std.e~2 robus~3 stati~4   p.value conf.~5 conf.~6
  <chr>                  <dbl>   <dbl>   <dbl>   <dbl>     <dbl>   <dbl>   <dbl>
1 tr_outcome.lag         0.661 0.0144  0.0197   -21.0  5.52e- 98   0.636   0.687
2 comp_bpsc_y3_severe~   0.913 0.0144  0.0193    -4.72 2.40e-  6   0.879   0.948
3 less_90d_tr1.lag       0.871 0.0180  0.0248    -5.58 2.38e-  8   0.829   0.914
4 log_dias_treat_imp_~   0.829 0.00303 0.0121   -15.5  4.29e- 54   0.810   0.849
5 policonsumo2.lag       0.531 0.0144  0.0239   -26.5  4.80e-155   0.507   0.556
6 edad_al_ing_1          1.07  0.00153 0.00140   50.1  0           1.07    1.08 
7 ano_nac_corr           1.09  0.00152 0.00140   58.9  0           1.08    1.09 
# ... with abbreviated variable names 1: estimate, 2: std.error, 3: robust.se,
#   4: statistic, 5: conf.low, 6: conf.high
Code
#   term                          estimate std.error robust.se statistic  p.value conf.low conf.high
#   <chr>                            <dbl>     <dbl>     <dbl>     <dbl>    <dbl>    <dbl>     <dbl>
# 1 tr_outcome.lag                   0.551   0.0133    0.0152     -39.3  0           0.534     0.567
# 2 comp_bpsc_y3_severe.lag          0.866   0.0142    0.0162      -8.88 6.86e-19    0.839     0.894
# 3 less_90d_tr1.lag                 0.904   0.0184    0.0263      -3.83 1.31e- 4    0.859     0.952
# 4 log_dias_treat_imp_sin_na.lag    0.873   0.00421   0.0137      -9.86 6.23e-23    0.850     0.897
# 5 policonsumo2.lag                 0.450   0.0135    0.0167     -47.9  0           0.435     0.465
# 6 edad_al_ing_1                    1.07    0.00153   0.00138     51.0  0           1.07      1.08 
# 7 ano_nac_corr                     1.09    0.00152   0.00138     60.0  0           1.08      1.09 
broom::tidy(final_restr_b$m, conf.int=T, exponentiate =T)
# A tibble: 7 x 8
  term                 estim~1 std.e~2 robus~3 stati~4   p.value conf.~5 conf.~6
  <chr>                  <dbl>   <dbl>   <dbl>   <dbl>     <dbl>   <dbl>   <dbl>
1 tr_outcome.lag         0.679 0.0150  0.0185   -21.0  1.78e- 97   0.654   0.704
2 comp_bpsc_y3_severe~   0.937 0.0146  0.0178    -3.67 2.39e-  4   0.904   0.970
3 less_90d_tr1.lag       0.857 0.0181  0.0246    -6.29 3.10e- 10   0.816   0.899
4 log_dias_treat_imp_~   0.835 0.00326 0.0130   -13.9  6.21e- 44   0.814   0.857
5 policonsumo2.lag       0.576 0.0152  0.0211   -26.1  2.67e-150   0.552   0.600
6 edad_al_ing_1          1.14  0.00293 0.00274   47.3  0           1.13    1.14 
7 ano_nac_corr           1.15  0.00293 0.00268   52.1  0           1.14    1.16 
# ... with abbreviated variable names 1: estimate, 2: std.error, 3: robust.se,
#   4: statistic, 5: conf.low, 6: conf.high
Code
# term                          estimate std.error robust.se statistic   p.value conf.low conf.high
# <chr>                            <dbl>     <dbl>     <dbl>     <dbl>     <dbl>    <dbl>     <dbl>
# tr_outcome.lag                   0.603   0.0144    0.0155     -32.6  1.17e-232    0.585     0.621
# comp_bpsc_y3_severe.lag          0.913   0.0144    0.0149      -6.14 8.03e- 10    0.886     0.940
# less_90d_tr1.lag                 0.898   0.0187    0.0249      -4.34 1.45e-  5    0.855     0.943
# log_dias_treat_imp_sin_na.lag    0.887   0.00464   0.0134      -8.92 4.53e- 19    0.864     0.911
# policonsumo2.lag                 0.514   0.0146    0.0163     -40.9  0            0.498     0.530
# edad_al_ing_1                    1.13    0.00291   0.00250     48.1  0            1.12      1.13 
# ano_nac_corr                     1.14    0.00291   0.00249     52.9  0            1.14      1.15 


final_coefs<-
cbind.data.frame(term = c("tr_outcome.lag", "comp_bpsc_y3_severe.lag", "less_90d_tr1.lag", "policonsumo2.lag", "edad_al_ing_1", "ano_nac_corr"), estimate = c(1.1680161176676, 1.79963517024553, 2.2710236051536, 1.17641492065206, 1.08756180792251, 1.09565232375642), std.error = c(0.0188883080181889, 0.0125981011876289, 0.0129873997440068, 0.0189035437658918, 0.00153425515290826, 0.00153498904364182), robust.se = c(0.0172377882900819, 0.0129754816518389, 0.0142405606352216, 0.0195792679568791, 0.00139105857932987, 0.00138071917329718), statistic = c(9.0096641790833, 45.2841734070737, 57.5981998354651, 8.29814532853555, 60.3413245741051, 66.1611116269822), p.value = c(2.06688423909932e-19, 0, 0, 1.05749268471728e-16, 0, 0), conf.low = c(1.12921334955458, 1.75444485412689, 2.20851351433301, 1.13212562626954, 1.0846006909948, 1.09269132170511), conf.high = c(1.20815225189237, 1.84598948115495, 2.33530299076412, 1.22243683335131, 1.09053100912818, 1.09862134960454))

summary(final$iiw)
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
 0.07435  1.00000  1.00000  0.90727  1.00000 68.81131 
Code
# 
   # Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   #  0.1     1.0     1.0     0.9     1.0    67.9 

summary(final_restr$iiw)
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
 0.08549  0.44872  0.84983  0.74315  1.00000 22.25216 
Code
   # Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   # 0.09    0.45    0.85    0.74    1.00   22.04 

invisible("Generar los pesos a asignar")
table(final$iiw, data_mine_miss_proc$treatment) %>% data.frame() %>% dplyr::mutate(Var1=as.numeric(as.character(Var1)), Freq=as.numeric(Freq)) %>% dplyr::arrange(-Freq) %>% dplyr::select(-Var2) %>%  head(20) %>% dput()
structure(list(Var1 = c(1, 0.2934922955593, 0.0743489610317217, 
0.0757106912282225, 0.0762091661974078, 0.0783327140965704, 0.0803105805175816, 
0.0803344064102367, 0.0804539791818909, 0.0820904544526977, 0.0841827879338751, 
0.0844743814169596, 0.0849411523170872, 0.0851903256041754, 0.0855473256157542, 
0.0865205249520326, 0.0867570352679417, 0.088066938352575, 0.0880950865189643, 
0.0881021667861931), Freq = c(72404, 2, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), row.names = c(NA, 20L), class = "data.frame")
Code
df_iiw_final<-
cbind.data.frame(Var1 = c(1, 0.688125981472609, 0.160330998755955, 0.162315144547036, 0.196774517463519, 0.199606787793155, 0.208958220652199, 0.225731677712565, 0.23160770335121, 0.234224555582056, 0.248919669741449, 0.256310292435046, 0.275606737385485, 0.286468644357305, 0.291310136239186, 0.313585733564029, 0.314758256721433, 0.325438651308499, 0.329390866243409, 
0.334785138867613), Freq = c(72404, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2))


invisible("Vemos un ejemplo que los pesos asignados son ABSOLUTAMENTE DISTINTOS DEPENDIENDO DEL MODELO")
cbind.data.frame(mod1= final$iiw, mod2= final_b$iiw) %>%  dplyr::filter(mod1!=mod2) %>% head(20)
        mod1     mod2
1  0.1996962 3.830127
2  0.6129391 4.376810
3  0.4980109 5.125454
4  0.2382301 4.547194
5  0.5663714 3.425793
6  0.2268115 4.153721
7  0.1720342 5.678893
8  0.4969182 5.333688
9  0.1529589 3.750321
10 0.6696665 3.895218
11 0.5430455 2.042192
12 0.3793116 3.575538
13 0.4025839 3.457417
14 0.3729438 4.203298
15 0.4674447 2.872873
16 0.2808815 2.449836
17 0.5428789 8.560529
18 0.4052071 8.143959
19 0.7632280 7.063683
20 0.5840069 1.996160
Code
diff_in_iiw_final<-
cbind.data.frame(mod1 = c(0.199498133334888, 0.615044327911636, 0.498702140851273, 0.238887400402573, 0.564883027201795, 0.226856508027775, 0.172052897285973, 0.495624040479963, 0.152900128488306, 0.669574361459713, 0.543818575958686, 0.379205064180558, 0.402370920088421, 0.373500499352896, 0.467980694989343, 0.281860985585125, 0.544134768464911, 0.404800245361857, 0.763907890441244, 0.585275800391092), mod2 = c(3.92318595976502, 3.44378094343031, 4.97127471370872, 4.5599185879251, 3.04873855278048, 
3.31979843463701, 6.0865617052278, 5.81549682361537, 4.09528203750446, 3.99883088697191, 1.7257302695626, 3.11919908361139, 3.04433093376514, 4.05407333418453, 2.28706611143663, 2.08872102380155, 8.02426306030948, 8.36617898514813, 6.98296103354168, 1.33559630246317))
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

require(rms)
cox_iiw_proposed<-
rms::cph(Surv(lag_time,time,event)~ 
          cluster(id)+
          lag_tr_outcome+ 
          lag_less_90d_tr1+
          log_lag_dias_treat_imp_sin_na+
          lag_comp_bpsc_y3_severe+ 
          lag_policonsumo2+ 
          edad_al_ing_1+ 
          ano_nac_corr+ 
          susinidum_oh +
          susinidum_coc +
          susinidum_pbc +
          susinidum_mar+
          psycom_dum_with+
          psycom_dum_study+ 
          freq_cons_dum_5day+
          cond_oc_dum_2inact+
          cond_oc_dum_3unemp+
          susprindum_oh +
          susprindum_coc +
          susprindum_pbc +
          susprindum_mar +
          strat(tipo_de_plan_2_mod), 
          data = data_mine_miss_proc2, 
          x=TRUE, 
          y=TRUE,
          surv= TRUE)
#Must use x=TRUE,y=TRUE to get survival curves with time-dep. covariables
cox_iiw_proposed
Frequencies of Missing Values Due to Each Variable
  Surv(lag_time, time, event)                   cluster(id) 
                        72404                             0 
               lag_tr_outcome              lag_less_90d_tr1 
                            0                             0 
log_lag_dias_treat_imp_sin_na       lag_comp_bpsc_y3_severe 
                        72404                             0 
             lag_policonsumo2                 edad_al_ing_1 
                            0                             0 
                 ano_nac_corr                  susinidum_oh 
                            0                             0 
                susinidum_coc                 susinidum_pbc 
                            0                             0 
                susinidum_mar               psycom_dum_with 
                            0                             0 
             psycom_dum_study            freq_cons_dum_5day 
                            0                             0 
           cond_oc_dum_2inact            cond_oc_dum_3unemp 
                            0                             0 
                susprindum_oh                susprindum_coc 
                            0                             0 
               susprindum_pbc                susprindum_mar 
                            0                             0 
           tipo_de_plan_2_mod 
                            0 

Cox Proportional Hazards Model
 
 rms::cph(formula = Surv(lag_time, time, event) ~ cluster(id) + 
     lag_tr_outcome + lag_less_90d_tr1 + log_lag_dias_treat_imp_sin_na + 
     lag_comp_bpsc_y3_severe + lag_policonsumo2 + edad_al_ing_1 + 
     ano_nac_corr + susinidum_oh + susinidum_coc + susinidum_pbc + 
     susinidum_mar + psycom_dum_with + psycom_dum_study + freq_cons_dum_5day + 
     cond_oc_dum_2inact + cond_oc_dum_3unemp + susprindum_oh + 
     susprindum_coc + susprindum_pbc + susprindum_mar + strat(tipo_de_plan_2_mod), 
     data = data_mine_miss_proc2, x = TRUE, y = TRUE, surv = TRUE)
 
 
                                             Status
 Stratum                                      No Event Event
   tipo_de_plan_2_mod=basic ambulatory               0  5622
   tipo_de_plan_2_mod=GP intensive ambulatory        0  6501
   tipo_de_plan_2_mod=GP residential                 0  3034
   tipo_de_plan_2_mod=WO intensive ambulatory        0  1015
   tipo_de_plan_2_mod=WO residential                 0  1499
 
                           Model Tests       Discrimination    
                                                    Indexes    
 Obs       17671    LR chi2    3826.50       R2       0.195    
 Events    17671    d.f.            20    R2(20,17671)0.194    
 Center 474.9181    Pr(> chi2)  0.0000       Dxy      0.255    
                    Score chi2 3855.18                         
                    Pr(> chi2)  0.0000                         
 
                               Coef    S.E.   Wald Z Pr(>|Z|)
 lag_tr_outcome                 0.1545 0.0176  8.76  <0.0001 
 lag_less_90d_tr1               0.1033 0.0242  4.27  <0.0001 
 log_lag_dias_treat_imp_sin_na -0.0222 0.0100 -2.22  0.0263  
 lag_comp_bpsc_y3_severe        0.0584 0.0165  3.54  0.0004  
 lag_policonsumo2              -0.0181 0.0186 -0.97  0.3314  
 edad_al_ing_1                  0.2345 0.0039 60.56  <0.0001 
 ano_nac_corr                   0.2359 0.0038 61.97  <0.0001 
 susinidum_oh                   0.0441 0.0465  0.95  0.3436  
 susinidum_coc                  0.1096 0.0573  1.91  0.0558  
 susinidum_pbc                  0.1586 0.0527  3.01  0.0026  
 susinidum_mar                  0.1589 0.0474  3.35  0.0008  
 psycom_dum_with                0.0174 0.0161  1.08  0.2783  
 psycom_dum_study               0.0304 0.0224  1.36  0.1741  
 freq_cons_dum_5day             0.0135 0.0155  0.87  0.3834  
 cond_oc_dum_2inact             0.0581 0.0214  2.72  0.0066  
 cond_oc_dum_3unemp             0.0621 0.0171  3.63  0.0003  
 susprindum_oh                 -0.1040 0.0626 -1.66  0.0968  
 susprindum_coc                -0.1105 0.0636 -1.74  0.0822  
 susprindum_pbc                -0.0906 0.0622 -1.46  0.1452  
 susprindum_mar                -0.1151 0.0688 -1.67  0.0945  
 
Code
# Cox Proportional Hazards Model
# 
# rms::cph(formula = Surv(lag_time, time, event) ~ lag_tr_outcome + 
#     lag_less_90d_tr1 + log_lag_dias_treat_imp_sin_na + lag_comp_bpsc_y3_severe + 
#     lag_policonsumo2 + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
#     susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
#     psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
#     cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
#     susprindum_mar + cluster(id) + strat(tipo_de_plan_2_mod), 
#     data = data_mine_miss_proc2, x = TRUE, y = TRUE, surv = TRUE)
# 
# 
#                                             Status
# Stratum                                      No Event Event
#   tipo_de_plan_2_mod=basic ambulatory               0  5622
#   tipo_de_plan_2_mod=GP intensive ambulatory        0  6501
#   tipo_de_plan_2_mod=GP residential                 0  3034
#   tipo_de_plan_2_mod=WO intensive ambulatory        0  1015
#   tipo_de_plan_2_mod=WO residential                 0  1499
# 
#                           Model Tests       Discrimination    
#                                                    Indexes    
# Obs       17671    LR chi2    3826.50       R2       0.195    
# Events    17671    d.f.            20    R2(20,17671)0.194    
# Center 474.9181    Pr(> chi2)  0.0000       Dxy      0.255    
#                    Score chi2 3855.18                         
#                    Pr(> chi2)  0.0000                         
# 
#                               Coef    S.E.   Wald Z Pr(>|Z|)
# lag_tr_outcome                 0.1545 0.0176  8.76  <0.0001 
# lag_less_90d_tr1               0.1033 0.0242  4.27  <0.0001 
# log_lag_dias_treat_imp_sin_na -0.0222 0.0100 -2.22  0.0263  
# lag_comp_bpsc_y3_severe        0.0584 0.0165  3.54  0.0004  
# lag_policonsumo2              -0.0181 0.0186 -0.97  0.3314  
# edad_al_ing_1                  0.2345 0.0039 60.56  <0.0001 
# ano_nac_corr                   0.2359 0.0038 61.97  <0.0001 
# susinidum_oh                   0.0441 0.0465  0.95  0.3436  
# susinidum_coc                  0.1096 0.0573  1.91  0.0558  
# susinidum_pbc                  0.1586 0.0527  3.01  0.0026  
# susinidum_mar                  0.1589 0.0474  3.35  0.0008  
# psycom_dum_with                0.0174 0.0161  1.08  0.2783  
# psycom_dum_study               0.0304 0.0224  1.36  0.1741  
# freq_cons_dum_5day             0.0135 0.0155  0.87  0.3834  
# cond_oc_dum_2inact             0.0581 0.0214  2.72  0.0066  
# cond_oc_dum_3unemp             0.0621 0.0171  3.63  0.0003  
# susprindum_oh                 -0.1040 0.0626 -1.66  0.0968  
# susprindum_coc                -0.1105 0.0636 -1.74  0.0822  
# susprindum_pbc                -0.0906 0.0622 -1.46  0.1452  
# susprindum_mar                -0.1151 0.0688 -1.67  0.0945 

round(cox.zph(cox_iiw_proposed)$table, 4)
                                 chisq df      p
lag_tr_outcome                 53.1085  1 0.0000
lag_less_90d_tr1              130.0606  1 0.0000
log_lag_dias_treat_imp_sin_na 142.4076  1 0.0000
lag_comp_bpsc_y3_severe        20.3681  1 0.0000
lag_policonsumo2                8.3301  1 0.0039
edad_al_ing_1                  33.2395  1 0.0000
ano_nac_corr                    7.7556  1 0.0054
susinidum_oh                   40.0796  1 0.0000
susinidum_coc                   0.9605  1 0.3271
susinidum_pbc                   6.6977  1 0.0097
susinidum_mar                  24.9223  1 0.0000
psycom_dum_with                16.2090  1 0.0001
psycom_dum_study               37.2938  1 0.0000
freq_cons_dum_5day              2.1105  1 0.1463
cond_oc_dum_2inact              3.1074  1 0.0779
cond_oc_dum_3unemp             14.3746  1 0.0001
susprindum_oh                  24.9308  1 0.0000
susprindum_coc                  0.7316  1 0.3924
susprindum_pbc                 19.7982  1 0.0000
susprindum_mar                  2.6082  1 0.1063
GLOBAL                        495.5153 20 0.0000
Code
# lag_tr_outcome                 53.1085  1 0.0000 ###
# lag_less_90d_tr1              130.0606  1 0.0000 ###
# log_lag_dias_treat_imp_sin_na 142.4076  1 0.0000 ###
# lag_comp_bpsc_y3_severe        20.3681  1 0.0000 ###
# lag_policonsumo2                8.3301  1 0.0039 #
# edad_al_ing_1                  33.2395  1 0.0000 ###
# ano_nac_corr                    7.7556  1 0.0054 #
# susinidum_oh                   40.0796  1 0.0000 ###
# susinidum_coc                   0.9605  1 0.3271
# susinidum_pbc                   6.6977  1 0.0097
# susinidum_mar                  24.9223  1 0.0000 ###
# psycom_dum_with                16.2090  1 0.0001 ###
# psycom_dum_study               37.2938  1 0.0000 ###
# freq_cons_dum_5day              2.1105  1 0.1463
# cond_oc_dum_2inact              3.1074  1 0.0779
# cond_oc_dum_3unemp             14.3746  1 0.0001 ###
# susprindum_oh                  24.9308  1 0.0000 ###
# susprindum_coc                  0.7316  1 0.3924
# susprindum_pbc                 19.7982  1 0.0000 ###
# susprindum_mar                  2.6082  1 0.1063
# GLOBAL                        495.5153 20 0.0000 

cox_iiw_restr_proposed<-
rms::cph(Surv(lag_time,time,event)~ 
          cluster(id)+ 
          lag_tr_outcome+ 
          lag_less_90d_tr1+
          log_lag_dias_treat_imp_sin_na+
          lag_comp_bpsc_y3_severe+ 
          lag_policonsumo2+ 
          edad_al_ing_1+ 
          ano_nac_corr+ 
          susinidum_oh +
          susinidum_coc +
          susinidum_pbc +
          susinidum_mar+
          psycom_dum_with+
          psycom_dum_study+ 
          freq_cons_dum_5day+
          cond_oc_dum_2inact+
          cond_oc_dum_3unemp+
          susprindum_oh +
          susprindum_coc +
          susprindum_pbc +
          susprindum_mar +
          strat(tipo_de_plan_2_mod), 
          data = data_mine_miss_restr_proc2, 
          x=TRUE, 
          y=TRUE,
          surv= TRUE)
#Must use x=TRUE,y=TRUE to get survival curves with time-dep. covariables
cox_iiw_restr_proposed
Frequencies of Missing Values Due to Each Variable
  Surv(lag_time, time, event)                   cluster(id) 
                        13317                             0 
               lag_tr_outcome              lag_less_90d_tr1 
                            0                             0 
log_lag_dias_treat_imp_sin_na       lag_comp_bpsc_y3_severe 
                        13317                             0 
             lag_policonsumo2                 edad_al_ing_1 
                            0                             0 
                 ano_nac_corr                  susinidum_oh 
                            0                             0 
                susinidum_coc                 susinidum_pbc 
                            0                             0 
                susinidum_mar               psycom_dum_with 
                            0                             0 
             psycom_dum_study            freq_cons_dum_5day 
                            0                             0 
           cond_oc_dum_2inact            cond_oc_dum_3unemp 
                            0                             0 
                susprindum_oh                susprindum_coc 
                            0                             0 
               susprindum_pbc                susprindum_mar 
                            0                             0 
           tipo_de_plan_2_mod 
                            0 

Cox Proportional Hazards Model
 
 rms::cph(formula = Surv(lag_time, time, event) ~ cluster(id) + 
     lag_tr_outcome + lag_less_90d_tr1 + log_lag_dias_treat_imp_sin_na + 
     lag_comp_bpsc_y3_severe + lag_policonsumo2 + edad_al_ing_1 + 
     ano_nac_corr + susinidum_oh + susinidum_coc + susinidum_pbc + 
     susinidum_mar + psycom_dum_with + psycom_dum_study + freq_cons_dum_5day + 
     cond_oc_dum_2inact + cond_oc_dum_3unemp + susprindum_oh + 
     susprindum_coc + susprindum_pbc + susprindum_mar + strat(tipo_de_plan_2_mod), 
     data = data_mine_miss_restr_proc2, x = TRUE, y = TRUE, surv = TRUE)
 
 
                                             Status
 Stratum                                      No Event Event
   tipo_de_plan_2_mod=basic ambulatory               0  5633
   tipo_de_plan_2_mod=GP intensive ambulatory        0  6499
   tipo_de_plan_2_mod=GP residential                 0  3026
   tipo_de_plan_2_mod=WO intensive ambulatory        0  1015
   tipo_de_plan_2_mod=WO residential                 0  1498
 
                           Model Tests       Discrimination    
                                                    Indexes    
 Obs       17671    LR chi2    3826.06       R2       0.195    
 Events    17671    d.f.            20    R2(20,17671)0.194    
 Center 474.8266    Pr(> chi2)  0.0000       Dxy      0.255    
                    Score chi2 3854.74                         
                    Pr(> chi2)  0.0000                         
 
                               Coef    S.E.   Wald Z Pr(>|Z|)
 lag_tr_outcome                 0.1540 0.0176  8.73  <0.0001 
 lag_less_90d_tr1               0.1036 0.0242  4.28  <0.0001 
 log_lag_dias_treat_imp_sin_na -0.0223 0.0100 -2.23  0.0257  
 lag_comp_bpsc_y3_severe        0.0590 0.0165  3.58  0.0003  
 lag_policonsumo2              -0.0181 0.0186 -0.97  0.3312  
 edad_al_ing_1                  0.2345 0.0039 60.55  <0.0001 
 ano_nac_corr                   0.2359 0.0038 61.96  <0.0001 
 susinidum_oh                   0.0441 0.0465  0.95  0.3434  
 susinidum_coc                  0.1097 0.0573  1.91  0.0555  
 susinidum_pbc                  0.1589 0.0527  3.01  0.0026  
 susinidum_mar                  0.1589 0.0474  3.35  0.0008  
 psycom_dum_with                0.0174 0.0161  1.09  0.2778  
 psycom_dum_study               0.0303 0.0224  1.35  0.1762  
 freq_cons_dum_5day             0.0139 0.0155  0.89  0.3716  
 cond_oc_dum_2inact             0.0581 0.0214  2.72  0.0065  
 cond_oc_dum_3unemp             0.0625 0.0171  3.66  0.0003  
 susprindum_oh                 -0.1038 0.0626 -1.66  0.0973  
 susprindum_coc                -0.1104 0.0636 -1.74  0.0826  
 susprindum_pbc                -0.0902 0.0622 -1.45  0.1467  
 susprindum_mar                -0.1150 0.0688 -1.67  0.0947  
 
Code
# Cox Proportional Hazards Model
# 
# rms::cph(formula = Surv(lag_time, time, event) ~ lag_tr_outcome + 
#     lag_less_90d_tr1 + log_lag_dias_treat_imp_sin_na + lag_comp_bpsc_y3_severe + 
#     lag_policonsumo2 + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
#     susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
#     psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
#     cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
#     susprindum_mar + cluster(id) + strat(tipo_de_plan_2_mod), 
#     data = data_mine_miss_restr_proc2, x = TRUE, y = TRUE, surv = TRUE)
# 
# 
#                                             Status
# Stratum                                      No Event Event
#   tipo_de_plan_2_mod=basic ambulatory               0  5622
#   tipo_de_plan_2_mod=GP intensive ambulatory        0  6501
#   tipo_de_plan_2_mod=GP residential                 0  3034
#   tipo_de_plan_2_mod=WO intensive ambulatory        0  1015
#   tipo_de_plan_2_mod=WO residential                 0  1499
# 
#                           Model Tests       Discrimination    
#                                                    Indexes    
# Obs       17671    LR chi2    3826.50       R2       0.195    
# Events    17671    d.f.            20    R2(20,17671)0.194    
# Center 474.9181    Pr(> chi2)  0.0000       Dxy      0.255    
#                    Score chi2 3855.18                         
#                    Pr(> chi2)  0.0000                         
# 
#                               Coef    S.E.   Wald Z Pr(>|Z|)
# lag_tr_outcome                 0.1545 0.0176  8.76  <0.0001 
# lag_less_90d_tr1               0.1033 0.0242  4.27  <0.0001 
# log_lag_dias_treat_imp_sin_na -0.0222 0.0100 -2.22  0.0263  
# lag_comp_bpsc_y3_severe        0.0584 0.0165  3.54  0.0004  
# lag_policonsumo2              -0.0181 0.0186 -0.97  0.3314  
# edad_al_ing_1                  0.2345 0.0039 60.56  <0.0001 
# ano_nac_corr                   0.2359 0.0038 61.97  <0.0001 
# susinidum_oh                   0.0441 0.0465  0.95  0.3436  
# susinidum_coc                  0.1096 0.0573  1.91  0.0558  
# susinidum_pbc                  0.1586 0.0527  3.01  0.0026  
# susinidum_mar                  0.1589 0.0474  3.35  0.0008  
# psycom_dum_with                0.0174 0.0161  1.08  0.2783  
# psycom_dum_study               0.0304 0.0224  1.36  0.1741  
# freq_cons_dum_5day             0.0135 0.0155  0.87  0.3834  
# cond_oc_dum_2inact             0.0581 0.0214  2.72  0.0066  
# cond_oc_dum_3unemp             0.0621 0.0171  3.63  0.0003  
# susprindum_oh                 -0.1040 0.0626 -1.66  0.0968  
# susprindum_coc                -0.1105 0.0636 -1.74  0.0822  
# susprindum_pbc                -0.0906 0.0622 -1.46  0.1452  
# susprindum_mar                -0.1151 0.0688 -1.67  0.0945  
cox.zph(cox_iiw_restr_proposed)
                                chisq df       p
lag_tr_outcome                 52.745  1 3.8e-13
lag_less_90d_tr1              130.440  1 < 2e-16
log_lag_dias_treat_imp_sin_na 143.001  1 < 2e-16
lag_comp_bpsc_y3_severe        20.975  1 4.7e-06
lag_policonsumo2                8.383  1 0.00379
edad_al_ing_1                  33.324  1 7.8e-09
ano_nac_corr                    7.786  1 0.00526
susinidum_oh                   40.366  1 2.1e-10
susinidum_coc                   0.960  1 0.32728
susinidum_pbc                   6.850  1 0.00887
susinidum_mar                  24.998  1 5.7e-07
psycom_dum_with                16.153  1 5.8e-05
psycom_dum_study               37.275  1 1.0e-09
freq_cons_dum_5day              2.262  1 0.13261
cond_oc_dum_2inact              3.092  1 0.07866
cond_oc_dum_3unemp             14.720  1 0.00012
susprindum_oh                  25.145  1 5.3e-07
susprindum_coc                  0.758  1 0.38382
susprindum_pbc                 20.116  1 7.3e-06
susprindum_mar                  2.568  1 0.10906
GLOBAL                        497.106 20 < 2e-16
Code
#                                 chisq df       p
# lag_tr_outcome                 53.109  1 3.2e-13
# lag_less_90d_tr1              130.061  1 < 2e-16
# log_lag_dias_treat_imp_sin_na 142.408  1 < 2e-16
# lag_comp_bpsc_y3_severe        20.368  1 6.4e-06
# lag_policonsumo2                8.330  1 0.00390
# edad_al_ing_1                  33.239  1 8.1e-09
# ano_nac_corr                    7.756  1 0.00535
# susinidum_oh                   40.080  1 2.4e-10
# susinidum_coc                   0.961  1 0.32706
# susinidum_pbc                   6.698  1 0.00965
# susinidum_mar                  24.922  1 6.0e-07
# psycom_dum_with                16.209  1 5.7e-05
# psycom_dum_study               37.294  1 1.0e-09
# freq_cons_dum_5day              2.110  1 0.14630
# cond_oc_dum_2inact              3.107  1 0.07794
# cond_oc_dum_3unemp             14.375  1 0.00015
# susprindum_oh                  24.931  1 5.9e-07
# susprindum_coc                  0.732  1 0.39237
# susprindum_pbc                 19.798  1 8.6e-06
# susprindum_mar                  2.608  1 0.10631
# GLOBAL                        495.515 20 < 2e-16

invisible("552.545, schoenfeld residuals; now, 496")

res <- resid(cox_iiw_proposed, "scaledsch")
time <- as.numeric(dimnames(res)[[1]])


folder_path <- ifelse(dir.exists("E:/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/"),
                      "E:/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/",
                      "C:/Users/CISS Fondecyt/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/")
jpeg(filename = paste0(folder_path,"zph/schoefeld_res_all_variables_240421.jpg"), width = 8, height = 6, units = "in", res = 600, quality = 600)#Users/CISS Fondecyt/
par(mfrow = c(3, 3)) 

z2 <- loess(res[,"lag_tr_outcome"] ~ time, span=0.50)   # residuals 
plot(time, fitted(z2))
lines(supsmu(time, res[,"lag_tr_outcome"]),lty=2)
title("Sch. res., lag_tr_outcome")

z3 <- loess(res[,"lag_less_90d_tr1"] ~ time, span=0.50)   # residual
plot(time, fitted(z3))
lines(supsmu(time, res[,"lag_less_90d_tr1"]),lty=2)
title("Sch. res., lag_less_90d_tr1")

z4 <- loess(res[,"log_lag_dias_treat_imp_sin_na"] ~ time, span=0.50)   # residuals
plot(time, fitted(z4))
lines(supsmu(time, res[,"log_lag_dias_treat_imp_sin_na"]),lty=2)
title("Sch. res., log_lag_dias_treat_imp_sin_na")

z5 <- loess(res[,"lag_comp_bpsc_y3_severe"] ~ time, span=0.50)   # residuals
plot(time, fitted(z5))
lines(supsmu(time, res[,"lag_comp_bpsc_y3_severe"]),lty=2)
title("Sch. res., lag_comp_bpsc_y3_severe")

z6 <- loess(res[,"lag_policonsumo2"] ~ time, span=0.50)   # residuals
plot(time, fitted(z6))
lines(supsmu(time, res[,"lag_policonsumo2"]),lty=2)
title("Sch. res., lag_policonsumo2")

z7 <- loess(res[,"edad_al_ing_1"] ~ time, span=0.50)   # residuals
plot(time, fitted(z7))
lines(supsmu(time, res[,"edad_al_ing_1"]),lty=2)
title("Sch. res., edad_al_ing_1")

z8 <- loess(res[,"susinidum_oh"] ~ time, span=0.50)   # residuals 
plot(time, fitted(z8))
lines(supsmu(time, res[,"susinidum_oh"]),lty=2)
title("Sch. res., susinidum_oh")

z9 <- loess(res[,"psycom_dum_study"] ~ time, span=0.50)   # residuals
plot(time, fitted(z9))
lines(supsmu(time, res[,"psycom_dum_study"]),lty=2)
title("Sch. res., psycom_dum_study")

dev.off()
png 
  2 
Code
#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_}
#_#_#_#_#_#_#_#_#_#_#_}
invisible("Explore residuals in restricted subsample")
#_#_#_#_#_#_#_#_#_#_#_}#_#_#_#_#_#_#_#_#_#_#_}

res_restr <- resid(cox_iiw_restr_proposed, "scaledsch")
time_restr <- as.numeric(dimnames(res_restr)[[1]])


folder_path <- ifelse(dir.exists("E:/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/"),
                      "E:/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/",
                      "C:/Users/CISS Fondecyt/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/")
jpeg(filename = paste0(folder_path,"zph/schoefeld_res_all_variables_restr_240428.jpg"), width = 8, height = 6, units = "in", res = 600, quality = 600)#Users/CISS Fondecyt/
par(mfrow = c(3, 3)) 

z2_restr <- loess(res_restr[,"lag_tr_outcome"] ~ time, span=0.50)   # residuals 
plot(time_restr, fitted(z2_restr))
lines(supsmu(time_restr, res_restr[,"lag_tr_outcome"]),lty=2)
title("Sch. res., lag_tr_outcome")

z3_restr <- loess(res_restr[,"lag_less_90d_tr1"] ~ time, span=0.50)   # residual
plot(time_restr, fitted(z3_restr))
lines(supsmu(time_restr, res_restr[,"lag_less_90d_tr1"]),lty=2)
title("Sch. res., lag_less_90d_tr1")

z4_restr <- loess(res_restr[,"log_lag_dias_treat_imp_sin_na"] ~ time, span=0.50)   # residuals
plot(time, fitted(z4_restr))
lines(supsmu(time_restr, res_restr[,"log_lag_dias_treat_imp_sin_na"]),lty=2)
title("Sch. res., log_lag_dias_treat_imp_sin_na")

z5_restr <- loess(res_restr[,"lag_comp_bpsc_y3_severe"] ~ time, span=0.50)   # residuals
plot(time_restr, fitted(z5_restr))
lines(supsmu(time_restr, res_restr[,"lag_comp_bpsc_y3_severe"]),lty=2)
title("Sch. res., lag_comp_bpsc_y3_severe")

z6_restr <- loess(res_restr[,"lag_policonsumo2"] ~ time, span=0.50)   # residuals
plot(time_restr, fitted(z6_restr))
lines(supsmu(time_restr, res_restr[,"lag_policonsumo2"]),lty=2)
title("Sch. res., lag_policonsumo2")

z7_restr <- loess(res_restr[,"edad_al_ing_1"] ~ time, span=0.50)   # residuals
plot(time_restr, fitted(z7_restr))
lines(supsmu(time_restr, res_restr[,"edad_al_ing_1"]),lty=2)
title("Sch. res., edad_al_ing_1")

z8_restr <- loess(res_restr[,"susinidum_oh"] ~ time, span=0.50)   # residuals 
plot(time_restr, fitted(z8_restr))
lines(supsmu(time_restr, res_restr[,"susinidum_oh"]),lty=2)
title("Sch. res., susinidum_oh")

z9_restr <- loess(res_restr[,"psycom_dum_study"] ~ time, span=0.50)   # residuals
plot(time_restr, fitted(z9_restr))
lines(supsmu(time_restr, res_restr[,"psycom_dum_study"]),lty=2)
title("Sch. res., psycom_dum_study")

dev.off()
png 
  2 
Code
#save.image("C:/Users/CISS Fondecyt/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/20240422_avance_iiw.RData")

#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_}
plot(cox.zph(cox_iiw_proposed)[which(attr(cox_iiw_proposed$coefficients, "names") == "log_lag_dias_treat_imp_sin_na")], lwd=2)
abline(0,0, col="red", lty=3, lwd=2)
abline(h=cox_iiw_proposed$coefficients["log_lag_dias_treat_imp_sin_na"], col=3, lwd=2, lty= 3)
legend("bottomleft", legend= c("Reference line for the null effect", "Average hazard over time", "Time-varying hazard"), lty=c(3,2,1), col= c("red",3,1), lwd=2)

Code
#_#_#_#_#_#_#_#_#_#_#_}
km_days_tr <- survfit(Surv(lag_time,time,event)~ log_lag_dias_treat_imp_sin_na+
    cluster(id), data = data_mine_miss_proc2)

plot(km_days_tr, fun = "cloglog", xlab = "Time (in days) using log",
     ylab = "log-log survival", main = "log-log curves by lag_less_90d_tr1")
legend("bottomright", legend=c("=0", "=1"), col=c("black", "red"), lty=1)

Code
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
invisible("Restricted")

plot(cox.zph(cox_iiw_restr_proposed)[which(attr(cox_iiw_restr_proposed$coefficients, "names") == "log_lag_dias_treat_imp_sin_na")], lwd=2)
abline(0,0, col="red", lty=3, lwd=2)
abline(h=cox_iiw_restr_proposed$coefficients["log_lag_dias_treat_imp_sin_na"], col=3, lwd=2, lty= 3)
legend("bottomleft", legend= c("Reference line for the null effect", "Average hazard over time", "Time-varying hazard"), lty=c(3,2,1), col= c("red",3,1), lwd=2)

Code
#_#_#_#_#_#_#_#_#_#_#_}
km_days_tr_restr <- survfit(Surv(lag_time,time,event)~ log_lag_dias_treat_imp_sin_na+
    cluster(id), data = data_mine_miss_restr_proc2)

plot(km_days_tr_restr, fun = "cloglog", xlab = "Time (in days) using log",
     ylab = "log-log survival", main = "log-log curves by log_lag_dias_treat_imp_sin_na (restr)")

Code
#legend("bottomright", legend=c("=0", "=1"), col=c("black", "red"), lty=1)

Transforming variables

Code
#load("C:/Users/CISS Fondecyt/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/20240422_avance_iiw.RData")
#load("E:/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/20240422_avance_iiw.RData")
require(splines)

Loading required package: splines

Code
invisible("Primero necesito buscar log_lag_dias_treat_imp_sin_na , luego lag_comp_bpsc_y3_severe, edad_al_ing_1 y porc_pobr")
#specs_dias_treat <- paste0("log_lag_dias_treat_imp_sin_na *", c("time", "poly(time, 3)", "poly(time, 4)", "poly(time, 5)", "poly(time, 6)", "bs(time, 3)", "bs(time, 4)", "bs(time, 5)", "bs(time, 6)", "ns(time, 3)", "ns(time, 4)", "ns(time, 5)", "ns(time, 6)", "rcs(time, 3)", "rcs(time, 4)", "rcs(time, 5)", "rcs(time, 6)"))
specs_dias_treat <- paste0("", c("lag_dias_treat_imp_sin_na", "poly(ifelse(is.na(log_lag_dias_treat_imp_sin_na),.001,log_lag_dias_treat_imp_sin_na), 3)", "poly(ifelse(is.na(log_lag_dias_treat_imp_sin_na),.001,log_lag_dias_treat_imp_sin_na), 4)", "poly(ifelse(is.na(log_lag_dias_treat_imp_sin_na),.001,log_lag_dias_treat_imp_sin_na), 5)", "poly(ifelse(is.na(log_lag_dias_treat_imp_sin_na),.001,log_lag_dias_treat_imp_sin_na), 6)", "bs(log_lag_dias_treat_imp_sin_na, 3)", "bs(log_lag_dias_treat_imp_sin_na, 4)", "bs(log_lag_dias_treat_imp_sin_na, 5)", "bs(log_lag_dias_treat_imp_sin_na, 6)", "ns(log_lag_dias_treat_imp_sin_na, 3)", "ns(log_lag_dias_treat_imp_sin_na, 4)", "ns(log_lag_dias_treat_imp_sin_na, 5)", "ns(log_lag_dias_treat_imp_sin_na, 6)", "rcs(log_lag_dias_treat_imp_sin_na, 3)", "rcs(log_lag_dias_treat_imp_sin_na, 4)", "rcs(log_lag_dias_treat_imp_sin_na, 5)", "rcs(log_lag_dias_treat_imp_sin_na, 6)"))
aic_values0 <- vector(length=length(specs_dias_treat))
coxzph_values0 <-vector(length=length(specs_dias_treat))

for (i in seq_along(specs_dias_treat)) {
  l <- specs_dias_treat[i]
  # TryCatch block to handle errors during model fitting
  #result <- tryCatch({
    model <- cph(as.formula(paste("Surv(lag_time,time,event==1)~ 
            cluster(id) +
            lag_tr_outcome + 
            lag_less_90d_tr1 +",
            l,
            "lag_comp_bpsc_y3_severe + 
            lag_policonsumo2 + 
            edad_al_ing_1 + 
            ano_nac_corr + 
            susinidum_oh +
            susinidum_coc +
            susinidum_pbc +
            susinidum_mar +
            psycom_dum_with +
            psycom_dum_study + 
            freq_cons_dum_5day +
            cond_oc_dum_2inact +
            cond_oc_dum_3unemp +
            susprindum_oh +
            susprindum_coc +
            susprindum_pbc +
            susprindum_mar +
            strat(tipo_de_plan_2)", sep=" +")), 
           data=data_mine_miss_proc2, x=TRUE, y=TRUE, surv=TRUE)
    
    aic_val <- extractAIC(model)[[2]]
    cox_zph_val <- cox.zph(model)$table[nrow(cox.zph(model)$table), 1]

   # return(c(aic_val, cox_zph_val))
#  }, error = function(e) {
#    return(c(NA, NA))
 # })
    aic_values0[i] <- aic_val
    coxzph_values0[i] <- cox_zph_val
  print(cat(paste("Spec: ", l, "\n")))
  print(cat(paste("AIC: ", aic_val[1], "\n")))
  print(cat(paste("Cox zph test statistic: ", cox_zph_val[1], "\n")))     
}
Spec:  lag_dias_treat_imp_sin_na 
NULL
AIC:  258772.431420878 
NULL
Cox zph test statistic:  700.698782419001 
NULL
Spec:  poly(ifelse(is.na(log_lag_dias_treat_imp_sin_na),.001,log_lag_dias_treat_imp_sin_na), 3) 
NULL
AIC:  258752.758407148 
NULL
Cox zph test statistic:  681.320358260574 
NULL
Spec:  poly(ifelse(is.na(log_lag_dias_treat_imp_sin_na),.001,log_lag_dias_treat_imp_sin_na), 4) 
NULL
AIC:  258752.352037703 
NULL
Cox zph test statistic:  685.49092454129 
NULL
Spec:  poly(ifelse(is.na(log_lag_dias_treat_imp_sin_na),.001,log_lag_dias_treat_imp_sin_na), 5) 
NULL
AIC:  258754.237212064 
NULL
Cox zph test statistic:  688.359919424507 
NULL
Spec:  poly(ifelse(is.na(log_lag_dias_treat_imp_sin_na),.001,log_lag_dias_treat_imp_sin_na), 6) 
NULL
AIC:  258756.065355281 
NULL
Cox zph test statistic:  699.028740090731 
NULL
Spec:  bs(log_lag_dias_treat_imp_sin_na, 3) 
NULL
AIC:  258752.758407148 
NULL
Cox zph test statistic:  681.32035826058 
NULL
Spec:  bs(log_lag_dias_treat_imp_sin_na, 4) 
NULL
AIC:  258752.591529064 
NULL
Cox zph test statistic:  692.29531570604 
NULL
Spec:  bs(log_lag_dias_treat_imp_sin_na, 5) 
NULL
AIC:  258754.375773626 
NULL
Cox zph test statistic:  692.851794860437 
NULL
Spec:  bs(log_lag_dias_treat_imp_sin_na, 6) 
NULL
AIC:  258755.643123221 
NULL
Cox zph test statistic:  692.08808495201 
NULL
Spec:  ns(log_lag_dias_treat_imp_sin_na, 3) 
NULL
AIC:  258751.283635165 
NULL
Cox zph test statistic:  686.265288516322 
NULL
Spec:  ns(log_lag_dias_treat_imp_sin_na, 4) 
NULL
AIC:  258753.142150926 
NULL
Cox zph test statistic:  689.263524577162 
NULL
Spec:  ns(log_lag_dias_treat_imp_sin_na, 5) 
NULL
AIC:  258754.209648194 
NULL
Cox zph test statistic:  688.709704889273 
NULL
Spec:  ns(log_lag_dias_treat_imp_sin_na, 6) 
NULL
AIC:  258755.749792632 
NULL
Cox zph test statistic:  688.462498438466 
NULL
Spec:  rcs(log_lag_dias_treat_imp_sin_na, 3) 
NULL
AIC:  258770.719152328 
NULL
Cox zph test statistic:  684.053264675061 
NULL
Spec:  rcs(log_lag_dias_treat_imp_sin_na, 4) 
NULL
AIC:  258769.093325505 
NULL
Cox zph test statistic:  690.115532063767 
NULL
Spec:  rcs(log_lag_dias_treat_imp_sin_na, 5) 
NULL
AIC:  258770.713482787 
NULL
Cox zph test statistic:  693.692481658824 
NULL
Spec:  rcs(log_lag_dias_treat_imp_sin_na, 6) 
NULL
AIC:  258769.353464904 
NULL
Cox zph test statistic:  697.798435975237 
NULL
Code
message(paste0("There was no model that improved cox zph of the restricted sample (vs. ", round(cox.zph(cox_iiw_proposed)$table[nrow(cox.zph(cox_iiw_proposed)$table), 1],1),")"))

There was no model that improved cox zph of the restricted sample (vs. 495.5)

Code
#There was no model that improved cox zph of the restricted sample (vs. 495.5)

aic_values0_restr <- vector(length=length(specs_dias_treat))
coxzph_values0_restr <-vector(length=length(specs_dias_treat))

for (i in seq_along(specs_dias_treat)) {
  l <- specs_dias_treat[i]
  # TryCatch block to handle errors during model fitting
  #result <- tryCatch({
    model <- cph(as.formula(paste("Surv(lag_time,time,event==1)~ 
                cluster(id) +
                lag_tr_outcome + 
                lag_less_90d_tr1 +",
                "I(round(",l,",2))+",
                "lag_comp_bpsc_y3_severe + 
                lag_policonsumo2 + 
                edad_al_ing_1 + 
                ano_nac_corr + 
                susinidum_oh +
                susinidum_coc +
                susinidum_pbc +
                susinidum_mar +
                psycom_dum_with +
                psycom_dum_study + 
                freq_cons_dum_5day +
                cond_oc_dum_2inact +
                cond_oc_dum_3unemp +
                susprindum_oh +
                susprindum_coc +
                susprindum_pbc +
                susprindum_mar +
                strat(tipo_de_plan_2_mod)", sep="")), 
           data=data_mine_miss_restr_proc2, x=TRUE, y=TRUE, surv=TRUE)
    
    aic_val_restr <- extractAIC(model)[[2]]
    cox_zph_val_restr <- cox.zph(model)$table[nrow(cox.zph(model)$table), 1]

   # return(c(aic_val, cox_zph_val))
#  }, error = function(e) {
#    return(c(NA, NA))
 # })
    aic_values0_restr[i] <- aic_val_restr
    coxzph_values0_restr[i] <- cox_zph_val_restr
  print(cat(paste("Spec: ", l, "\n")))
  print(cat(paste("AIC: ", aic_val_restr[1], "\n")))
  print(cat(paste("Cox zph test statistic: ", cox_zph_val_restr[1], "\n")))     
}
Spec:  lag_dias_treat_imp_sin_na 
NULL
AIC:  246485.806233321 
NULL
Cox zph test statistic:  639.107234814594 
NULL
Spec:  poly(ifelse(is.na(log_lag_dias_treat_imp_sin_na),.001,log_lag_dias_treat_imp_sin_na), 3) 
NULL
AIC:  246485.395254038 
NULL
Cox zph test statistic:  563.585278389179 
NULL
Spec:  poly(ifelse(is.na(log_lag_dias_treat_imp_sin_na),.001,log_lag_dias_treat_imp_sin_na), 4) 
NULL
AIC:  246487.221628619 
NULL
Cox zph test statistic:  567.377829244832 
NULL
Spec:  poly(ifelse(is.na(log_lag_dias_treat_imp_sin_na),.001,log_lag_dias_treat_imp_sin_na), 5) 
NULL
AIC:  246487.489808795 
NULL
Cox zph test statistic:  589.036438983096 
NULL
Spec:  poly(ifelse(is.na(log_lag_dias_treat_imp_sin_na),.001,log_lag_dias_treat_imp_sin_na), 6) 
NULL
AIC:  246489.392166329 
NULL
Cox zph test statistic:  594.368973890416 
NULL
Spec:  bs(log_lag_dias_treat_imp_sin_na, 3) 
NULL
AIC:  246471.938352736 
NULL
Cox zph test statistic:  619.854794379174 
NULL
Spec:  bs(log_lag_dias_treat_imp_sin_na, 4) 
NULL
AIC:  246471.919669293 
NULL
Cox zph test statistic:  630.581400491839 
NULL
Spec:  bs(log_lag_dias_treat_imp_sin_na, 5) 
NULL
AIC:  246473.415412343 
NULL
Cox zph test statistic:  632.455278206685 
NULL
Spec:  bs(log_lag_dias_treat_imp_sin_na, 6) 
NULL
AIC:  246473.86359796 
NULL
Cox zph test statistic:  631.440180459019 
NULL
Spec:  ns(log_lag_dias_treat_imp_sin_na, 3) 
NULL
AIC:  246471.745534296 
NULL
Cox zph test statistic:  625.638986339057 
NULL
Spec:  ns(log_lag_dias_treat_imp_sin_na, 4) 
NULL
AIC:  246471.324101404 
NULL
Cox zph test statistic:  626.922786275545 
NULL
Spec:  ns(log_lag_dias_treat_imp_sin_na, 5) 
NULL
AIC:  246473.612068326 
NULL
Cox zph test statistic:  626.854990209742 
NULL
Spec:  ns(log_lag_dias_treat_imp_sin_na, 6) 
NULL
AIC:  246475.935992451 
NULL
Cox zph test statistic:  627.023802589597 
NULL
Spec:  rcs(log_lag_dias_treat_imp_sin_na, 3) 
NULL
AIC:  246481.982378402 
NULL
Cox zph test statistic:  625.011096881098 
NULL
Spec:  rcs(log_lag_dias_treat_imp_sin_na, 4) 
NULL
AIC:  246479.729583509 
NULL
Cox zph test statistic:  629.005973316837 
NULL
Spec:  rcs(log_lag_dias_treat_imp_sin_na, 5) 
NULL
AIC:  246480.719358582 
NULL
Cox zph test statistic:  633.068535007454 
NULL
Spec:  rcs(log_lag_dias_treat_imp_sin_na, 6) 
NULL
AIC:  246480.612072818 
NULL
Cox zph test statistic:  631.710467477473 
NULL
Code
message(paste0("There was no model that improved cox zph of the restricted sample (vs. ", round(cox.zph(cox_iiw_restr_proposed)$table[nrow(cox.zph(cox_iiw_restr_proposed)$table), 1],1),")"))

There was no model that improved cox zph of the restricted sample (vs. 497.1)

Code
#There was no model that improved cox zph of the restricted sample (vs. 495.5)

model_after_mod_log_lag_dias_treat<-
cph(Surv(lag_time,time,event)~ 
    cluster(id)+ 
    lag_tr_outcome +
    log_lag_dias_treat_imp_sin_na_rec.1+
    log_lag_dias_treat_imp_sin_na_rec.2+
    log_lag_dias_treat_imp_sin_na_rec.3+
    lag_less_90d_tr1+
    lag_comp_bpsc_y3_severe + 
    lag_policonsumo2 + 
    edad_al_ing_1 + 
    ano_nac_corr + 
    susinidum_oh +
    susinidum_coc +
    susinidum_pbc +
    susinidum_mar +
    psycom_dum_with +
    psycom_dum_study + 
    freq_cons_dum_5day +
    cond_oc_dum_2inact +
    cond_oc_dum_3unemp +
    susprindum_oh +
    susprindum_coc +
    susprindum_pbc +
    susprindum_mar +
      strat(tipo_de_plan_2), 
    data= data_mine_miss_proc2 %>% dplyr::mutate(log_lag_dias_treat_imp_sin_na_rec= bs(log_lag_dias_treat_imp_sin_na, 3)) %>% data.table::as.data.table() %>% data.frame(), x=TRUE, y=TRUE, surv=TRUE, iter.max = 250*4, tol = 1e-6)

cox.zph(model_after_mod_log_lag_dias_treat)
                                      chisq df       p
lag_tr_outcome                       42.103  1 8.7e-11
log_lag_dias_treat_imp_sin_na_rec.1 239.412  1 < 2e-16
log_lag_dias_treat_imp_sin_na_rec.2 275.169  1 < 2e-16
log_lag_dias_treat_imp_sin_na_rec.3 310.127  1 < 2e-16
lag_less_90d_tr1                    165.596  1 < 2e-16
lag_comp_bpsc_y3_severe              59.927  1 9.8e-15
lag_policonsumo2                     11.242  1 0.00080
edad_al_ing_1                        38.030  1 7.0e-10
ano_nac_corr                          9.980  1 0.00158
susinidum_oh                         48.294  1 3.7e-12
susinidum_coc                         0.687  1 0.40706
susinidum_pbc                        11.416  1 0.00073
susinidum_mar                        27.299  1 1.7e-07
psycom_dum_with                      11.212  1 0.00081
psycom_dum_study                     42.523  1 7.0e-11
freq_cons_dum_5day                   14.890  1 0.00011
cond_oc_dum_2inact                    0.241  1 0.62313
cond_oc_dum_3unemp                   28.081  1 1.2e-07
susprindum_oh                        35.197  1 3.0e-09
susprindum_coc                        1.806  1 0.17901
susprindum_pbc                       33.775  1 6.2e-09
susprindum_mar                        1.108  1 0.29243
GLOBAL                              681.320 22 < 2e-16
Code
invisible("The unadjusted was  547; now its 673 21 < 2e-16; we kept the simplest model")

#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_
invisible("Now we see less than 90 days in treatment interactions, given that the interaction with time of log tr. days did not improved the model")
par(mfrow=c(3,1))
par(mar = c(5, 4, 4, 5))
res240422_1 <- resid(cox_iiw_proposed, "scaledsch")
time240422_1 <- as.numeric(dimnames(res240422_1)[[1]])

z_240422_1_1 <- loess(res240422_1[,"lag_less_90d_tr1"] ~ time240422_1, span=0.50)   # residuals 
# x11() 
plot(time240422_1, fitted(z_240422_1_1))
lines(supsmu(time240422_1, res240422_1[,"psycom_dum_study"]),lty=2)
title("Sch. res., lag_less_90d_tr1")

plot(cox.zph(cox_iiw_proposed)[which(attr(cox_iiw_proposed$coefficients, "names") == "lag_less_90d_tr1")], lwd=2)
abline(0,0, col="red", lty=3, lwd=2)
abline(h=cox_iiw_proposed$coefficients["lag_less_90d_tr1"], col=3, lwd=2, lty= 3)
legend("bottomleft", legend= c("Reference line for the null effect", "Average hazard over time", "Time-varying hazard"), lty=c(3,2,1), col= c("red",3,1), lwd=2, bty = "n", bg = "transparent")
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

km_home <- survfit(Surv(lag_time,time,event)~ lag_less_90d_tr1+
    cluster(id), data = data_mine_miss_proc2)
#autoplot(km_home) # just to see the km curves
plot(km_home, fun = "cloglog", xlab = "Time (in days) using log",col=c("black", "red"),
     ylab = "log-log survival", main = "log-log curves by lag_less_90d_tr1")
legend("bottomright", legend=c("=0", "=1"), col=c("black", "red"), lty=1, bty = "n", bg = "transparent")

Code
recorded_plot <- recordPlot() 

folder_path <- ifelse(dir.exists("E:/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/"),
                      "E:/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/",
                      "C:/Users/CISS Fondecyt/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/")
png(paste0(folder_path,"zph/proportionallity_lag_less_90d_tr1.png"), height=13, width=10, res=500, units="in") 
recorded_plot
dev.off()
png 
  2 
Code
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
invisible("restricted")

par(mfrow=c(2,1))
par(mar = c(5, 4, 4, 5))


plot(cox.zph(cox_iiw_restr_proposed)[which(attr(cox_iiw_restr_proposed$coefficients, "names") == "lag_less_90d_tr1")], lwd=2)
abline(0,0, col="red", lty=3, lwd=2)
abline(h=cox_iiw_restr_proposed$coefficients["lag_less_90d_tr1"], col=3, lwd=2, lty= 3)
legend("bottomleft", legend= c("Reference line for the null effect", "Average hazard over time", "Time-varying hazard"), lty=c(3,2,1), col= c("red",3,1), lwd=2, bty = "n", bg = "transparent")
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

km_less90d_restr <- survfit(Surv(lag_time,time,event)~ lag_less_90d_tr1+
    cluster(id), data = data_mine_miss_restr_proc2)
#autoplot(km_home) # just to see the km curves
plot(km_less90d_restr, fun = "cloglog", xlab = "Time (in days) using log",col=c("black", "red"),
     ylab = "log-log survival", main = "log-log curves by lag_less_90d_tr1 (restr)")
legend("bottomright", legend=c("=0", "=1"), col=c("black", "red"), lty=1, bty = "n", bg = "transparent")

Code
recorded_plot <- recordPlot() 

folder_path <- ifelse(dir.exists("E:/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/"),
                      "E:/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/",
                      "C:/Users/CISS Fondecyt/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/")
png(paste0(folder_path,"zph/proportionallity_lag_less_90d_tr1_restr.png"), height=13, width=10, res=500, units="in") 
recorded_plot
dev.off()
png 
  2 
Code
create_time_varying_effect <- function(binary_covariate, t, decrease_end, increase_start, max_time) {
  # Initialize the effect to be equal to the covariate
  adjusted_effect <- binary_covariate
  
  # Decrease phase: a sharp decrease from the start until decrease_end
  # The covariate's effect is reduced to zero by decrease_end
  adjusted_effect <- ifelse(t <= decrease_end, binary_covariate * (decrease_end - t) / decrease_end, adjusted_effect)
  
  # Flat phase: between decrease_end and increase_start, the effect is zero
  adjusted_effect <- ifelse(t > decrease_end & t <= increase_start, 0, adjusted_effect)
  
  # Increase phase: a gradual increase from increase_start to max_time
  # The covariate's effect is increased back to its original value by max_time
  time_range <- max_time - increase_start
  adjusted_effect <- ifelse(t > increase_start, binary_covariate * (t - increase_start) / time_range, adjusted_effect)
  
  return(adjusted_effect)
}

data_mine_miss_proc2$lag_less_90d_tr1_rec<-
create_time_varying_effect(data_mine_miss_proc2$lag_less_90d_tr1, 
                           data_mine_miss_proc2$time, 
                           20,
                           80,
                           max(data_mine_miss_proc2$time))
data_mine_miss_restr_proc2$lag_less_90d_tr1_rec<-
create_time_varying_effect(data_mine_miss_restr_proc2$lag_less_90d_tr1, 
                           data_mine_miss_restr_proc2$time, 
                           20,
                           80,
                           max(data_mine_miss_restr_proc2$time))
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
invisible("Remove residuals")

mean_resid <- mean(res240422_1[,"lag_less_90d_tr1"], na.rm = TRUE)
sd_resid <- sd(res240422_1[,"lag_less_90d_tr1"], na.rm = TRUE)

# Identify outliers as those residuals greater than 3 standard deviations from the mean
outliers <- which(abs(res240422_1[,"lag_less_90d_tr1"] - mean_resid) > (3 * sd_resid))


data_clean <- data_mine_miss_proc2[-outliers, ]

invisible("even it worse results. Not important //DISCARDED")

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

invisible("Used a clog log plot, and told chat gpt to suggest a function")
#Here's a possible function that you can tailor to fit the divergence you observe in your log-log plot:
time_varying_coeff <- function(t, change_points, coefficients) {
    # t: vector of times
    # change_points: vector of times at which the hazard ratio changes
    # coefficients: vector of coefficients corresponding to intervals defined by change_points
    
    # Ensure change_points are sorted
    change_points <- sort(c(0, change_points, Inf))
    # Create a vector to hold the coefficient for each time t
    t_coeff <- rep(0, length(t))
    
    # Loop over the intervals defined by change_points
    for (i in seq_along(coefficients)) {
        interval_start <- change_points[i]
        interval_end <- change_points[i + 1]
        # Assign the coefficient to times within the current interval
        t_coeff[t > interval_start & t <= interval_end] <- coefficients[i]
    }
    
    return(t_coeff)
}

# Define the change points and coefficients based on your data and log-log plot
change_points <- c(10, 20, 30)  # Example change points (months)
coefficients <- c(0.5, 1.5, 0.5, 1)  # Example coefficients
# Add the time-varying coefficient to your model
data_mine_miss_proc2$lag_less_90d_tr1_rec2 <- with(data_mine_miss_proc2,
                                        lag_less_90d_tr1*time_varying_coeff(lag_time, change_points, coefficients))
data_mine_miss_proc2$less_90d_tr1_rec2 <- with(data_mine_miss_proc2,
                                        less_90d_tr1*time_varying_coeff(time, change_points, coefficients))

invisible("2024-04-30: restricted")
data_mine_miss_restr_proc2$lag_less_90d_tr1_rec2 <- with(data_mine_miss_restr_proc2,
                                        lag_less_90d_tr1*time_varying_coeff(lag_time, change_points, coefficients))
data_mine_miss_restr_proc2$less_90d_tr1_rec2 <- with(data_mine_miss_restr_proc2,
                                        less_90d_tr1*time_varying_coeff(time, change_points, coefficients))
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

model_after_mod_not_log_lag_dias_treat_lag_less_90d<-
cph(Surv(lag_time,time,event)~ 
    cluster(id)+ 
    lag_tr_outcome +
    log_lag_dias_treat_imp_sin_na +
    lag_less_90d_tr1_rec2+
    lag_comp_bpsc_y3_severe + 
    lag_policonsumo2 + 
    edad_al_ing_1 + 
    ano_nac_corr + 
    susinidum_oh +
    susinidum_coc +
    susinidum_pbc +
    susinidum_mar +
    psycom_dum_with +
    psycom_dum_study + 
    freq_cons_dum_5day +
    cond_oc_dum_2inact +
    cond_oc_dum_3unemp +
    susprindum_oh +
    susprindum_coc +
    susprindum_pbc +
    susprindum_mar+
      strat(tipo_de_plan_2), 
    #data= data_clean,
    data= data_mine_miss_proc2 %>% dplyr::mutate(log_lag_dias_treat_imp_sin_na_rec= bs(log_lag_dias_treat_imp_sin_na, 3)) %>% data.table::as.data.table() %>% data.frame(),
    x=TRUE, y=TRUE, surv=TRUE, iter.max = 250*4, tol = 1e-6)

cox.zph(model_after_mod_not_log_lag_dias_treat_lag_less_90d)
                                chisq df       p
lag_tr_outcome                 37.918  1 7.4e-10
log_lag_dias_treat_imp_sin_na 179.021  1 < 2e-16
lag_less_90d_tr1_rec2          89.397  1 < 2e-16
lag_comp_bpsc_y3_severe        59.737  1 1.1e-14
lag_policonsumo2               10.980  1 0.00092
edad_al_ing_1                  35.338  1 2.8e-09
ano_nac_corr                    8.684  1 0.00321
susinidum_oh                   45.623  1 1.4e-11
susinidum_coc                   0.773  1 0.37918
susinidum_pbc                  10.736  1 0.00105
susinidum_mar                  25.726  1 3.9e-07
psycom_dum_with                10.019  1 0.00155
psycom_dum_study               42.363  1 7.6e-11
freq_cons_dum_5day             15.415  1 8.6e-05
cond_oc_dum_2inact              0.113  1 0.73706
cond_oc_dum_3unemp             27.499  1 1.6e-07
susprindum_oh                  32.538  1 1.2e-08
susprindum_coc                  1.707  1 0.19134
susprindum_pbc                 31.291  1 2.2e-08
susprindum_mar                  1.017  1 0.31319
GLOBAL                        543.671 20 < 2e-16
Code
#                                 chisq df       p
# log_lag_dias_treat_imp_sin_na 163.562  1 < 2e-16
# lag_less_90d_tr1_rec2          85.349  1 < 2e-16
# lag_comp_bpsc_y3_severe        55.509  1 9.3e-14
# lag_policonsumo2               11.327  1 0.00076
# edad_al_ing_1                  33.351  1 7.7e-09
# ano_nac_corr                    7.573  1 0.00592
# susinidum_oh                   32.526  1 1.2e-08
# susinidum_coc                   0.831  1 0.36209
# susinidum_pbc                   2.136  1 0.14387
# susinidum_mar                  22.932  1 1.7e-06
# psycom_dum_with                10.244  1 0.00137
# psycom_dum_study               41.095  1 1.4e-10
# freq_cons_dum_5day             15.029  1 0.00011
# cond_oc_dum_2inact              0.171  1 0.67918
# cond_oc_dum_3unemp             27.481  1 1.6e-07
# susprindum_oh                  32.539  1 1.2e-08
# susprindum_coc                  1.498  1 0.22104
# susprindum_pbc                 30.540  1 3.3e-08
# susprindum_mar                  0.970  1 0.32457
# GLOBAL                        513.580 19 < 2e-16

message(paste0("There was no model that improved cox zph of the restricted sample (vs. ", round(cox.zph(cox_iiw_proposed)$table[nrow(cox.zph(cox_iiw_proposed)$table), 1],1),")"))

There was no model that improved cox zph of the restricted sample (vs. 495.5)

Code
invisible("We improved fit from 552 to 513")
invisible("We do not longer try to change log_lag_dias_treat_imp_sin_na_rec")

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
invisible("2024-04-30: restricted w/ tipo de plan 2 mod")

for (i in seq(.1,7,.1)) {
  change_points <- c(5*i, 10*i, 15*i)  # Example change points (months)
  coefficients <- c(0.5, 1.5, 0.5, 1)  # Example coefficients
  # Add the time-varying coefficient to your model
  data_mine_miss_restr_proc2$lag_less_90d_tr1_rec2 <- with(data_mine_miss_restr_proc2,
                                                           lag_less_90d_tr1*time_varying_coeff(lag_time, change_points, coefficients))
  data_mine_miss_restr_proc2$less_90d_tr1_rec2 <- with(data_mine_miss_restr_proc2,
                                                       less_90d_tr1*time_varying_coeff(lag_time, change_points, coefficients))
  
  model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d<-
    cph(as.formula(paste0("Surv(lag_time,time,event==1)~ 
              cluster(id)+ 
              lag_tr_outcome +
              log_lag_dias_treat_imp_sin_na +
              lag_less_90d_tr1_rec2+
              lag_comp_bpsc_y3_severe + 
              lag_policonsumo2 + 
              edad_al_ing_1 + 
              ano_nac_corr + 
              susinidum_oh +
              susinidum_coc +
              susinidum_pbc +
              susinidum_mar +
              psycom_dum_with +
              psycom_dum_study + 
              freq_cons_dum_5day +
              cond_oc_dum_2inact +
              cond_oc_dum_3unemp +
              susprindum_oh +
              susprindum_coc +
              susprindum_pbc +
              susprindum_mar+
              strat(tipo_de_plan_2_mod)")), 
        data=data_mine_miss_restr_proc2 %>% 
          data.table::as.data.table() %>% data.frame(), 
        x=TRUE, y=TRUE, surv=TRUE, iter.max = 250*4, tol = 1e-6)
  
  cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d)
  invisible("")
  print(paste0("iteration: ",i))
  print(change_points)
  print(format(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d)$table[nrow(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d)$table), 1]))
  
}
[1] "iteration: 0.1"
[1] 0.5 1.0 1.5
[1] "493.4811"
[1] "iteration: 0.2"
[1] 1 2 3
[1] "493.2048"
[1] "iteration: 0.3"
[1] 1.5 3.0 4.5
[1] "492.4265"
[1] "iteration: 0.4"
[1] 2 4 6
[1] "480.5242"
[1] "iteration: 0.5"
[1] 2.5 5.0 7.5
[1] "477.4635"
[1] "iteration: 0.6"
[1] 3 6 9
[1] "482.7047"
[1] "iteration: 0.7"
[1]  3.5  7.0 10.5
[1] "482.1585"
[1] "iteration: 0.8"
[1]  4  8 12
[1] "479.9969"
[1] "iteration: 0.9"
[1]  4.5  9.0 13.5
[1] "477.7753"
[1] "iteration: 1"
[1]  5 10 15
[1] "479.6782"
[1] "iteration: 1.1"
[1]  5.5 11.0 16.5
[1] "480.8076"
[1] "iteration: 1.2"
[1]  6 12 18
[1] "478.0329"
[1] "iteration: 1.3"
[1]  6.5 13.0 19.5
[1] "477.8009"
[1] "iteration: 1.4"
[1]  7 14 21
[1] "478.0451"
[1] "iteration: 1.5"
[1]  7.5 15.0 22.5
[1] "477.306"
[1] "iteration: 1.6"
[1]  8 16 24
[1] "476.7152"
[1] "iteration: 1.7"
[1]  8.5 17.0 25.5
[1] "475.7639"
[1] "iteration: 1.8"
[1]  9 18 27
[1] "477.6404"
[1] "iteration: 1.9"
[1]  9.5 19.0 28.5
[1] "478.1175"
[1] "iteration: 2"
[1] 10 20 30
[1] "478.6989"
[1] "iteration: 2.1"
[1] 10.5 21.0 31.5
[1] "477.3389"
[1] "iteration: 2.2"
[1] 11 22 33
[1] "477.742"
[1] "iteration: 2.3"
[1] 11.5 23.0 34.5
[1] "479.8333"
[1] "iteration: 2.4"
[1] 12 24 36
[1] "480.6214"
[1] "iteration: 2.5"
[1] 12.5 25.0 37.5
[1] "479.5284"
[1] "iteration: 2.6"
[1] 13 26 39
[1] "479.3746"
[1] "iteration: 2.7"
[1] 13.5 27.0 40.5
[1] "479.5649"
[1] "iteration: 2.8"
[1] 14 28 42
[1] "480.0663"
[1] "iteration: 2.9"
[1] 14.5 29.0 43.5
[1] "482.4381"
[1] "iteration: 3"
[1] 15 30 45
[1] "483.276"
[1] "iteration: 3.1"
[1] 15.5 31.0 46.5
[1] "485.369"
[1] "iteration: 3.2"
[1] 16 32 48
[1] "488.6019"
[1] "iteration: 3.3"
[1] 16.5 33.0 49.5
[1] "488.7055"
[1] "iteration: 3.4"
[1] 17 34 51
[1] "490.8723"
[1] "iteration: 3.5"
[1] 17.5 35.0 52.5
[1] "490.1774"
[1] "iteration: 3.6"
[1] 18 36 54
[1] "491.0848"
[1] "iteration: 3.7"
[1] 18.5 37.0 55.5
[1] "491.8408"
[1] "iteration: 3.8"
[1] 19 38 57
[1] "493.2258"
[1] "iteration: 3.9"
[1] 19.5 39.0 58.5
[1] "493.0619"
[1] "iteration: 4"
[1] 20 40 60
[1] "494.1005"
[1] "iteration: 4.1"
[1] 20.5 41.0 61.5
[1] "495.7924"
[1] "iteration: 4.2"
[1] 21 42 63
[1] "496.9032"
[1] "iteration: 4.3"
[1] 21.5 43.0 64.5
[1] "497.0522"
[1] "iteration: 4.4"
[1] 22 44 66
[1] "497.3046"
[1] "iteration: 4.5"
[1] 22.5 45.0 67.5
[1] "497.4102"
[1] "iteration: 4.6"
[1] 23 46 69
[1] "497.9092"
[1] "iteration: 4.7"
[1] 23.5 47.0 70.5
[1] "498.3643"
[1] "iteration: 4.8"
[1] 24 48 72
[1] "498.6149"
[1] "iteration: 4.9"
[1] 24.5 49.0 73.5
[1] "500.2164"
[1] "iteration: 5"
[1] 25 50 75
[1] "500.4883"
[1] "iteration: 5.1"
[1] 25.5 51.0 76.5
[1] "501.8345"
[1] "iteration: 5.2"
[1] 26 52 78
[1] "502.9337"
[1] "iteration: 5.3"
[1] 26.5 53.0 79.5
[1] "503.2239"
[1] "iteration: 5.4"
[1] 27 54 81
[1] "502.2895"
[1] "iteration: 5.5"
[1] 27.5 55.0 82.5
[1] "502.7881"
[1] "iteration: 5.6"
[1] 28 56 84
[1] "503.4912"
[1] "iteration: 5.7"
[1] 28.5 57.0 85.5
[1] "503.9953"
[1] "iteration: 5.8"
[1] 29 58 87
[1] "504.2127"
[1] "iteration: 5.9"
[1] 29.5 59.0 88.5
[1] "504.5094"
[1] "iteration: 6"
[1] 30 60 90
[1] "504.3318"
[1] "iteration: 6.1"
[1] 30.5 61.0 91.5
[1] "503.4482"
[1] "iteration: 6.2"
[1] 31 62 93
[1] "503.827"
[1] "iteration: 6.3"
[1] 31.5 63.0 94.5
[1] "503.1115"
[1] "iteration: 6.4"
[1] 32 64 96
[1] "503.6159"
[1] "iteration: 6.5"
[1] 32.5 65.0 97.5
[1] "503.661"
[1] "iteration: 6.6"
[1] 33 66 99
[1] "503.5745"
[1] "iteration: 6.7"
[1]  33.5  67.0 100.5
[1] "503.7049"
[1] "iteration: 6.8"
[1]  34  68 102
[1] "503.1642"
[1] "iteration: 6.9"
[1]  34.5  69.0 103.5
[1] "503.5052"
[1] "iteration: 7"
[1]  35  70 105
[1] "502.6516"
Code
# [1] "iteration: 0.5"
# [1] 2.5 5.0 7.5
# [1] "475.9965"
message(paste0("There was no model that improved cox zph of the restricted sample (vs. ", round(cox.zph(cox_iiw_proposed)$table[nrow(cox.zph(cox_iiw_proposed)$table), 1],1),")"))

There was no model that improved cox zph of the restricted sample (vs. 495.5)

Code
#There was no model that improved cox zph of the restricted sample (vs. 495.5)


invisible("Grid search of reduction of TVCs")

for (i in seq(.1,1.5,.05)) {
  tvcs <- c(20,80)*i

data_mine_miss_restr_proc2$lag_less_90d_tr1_rec<-
create_time_varying_effect(data_mine_miss_restr_proc2$lag_less_90d_tr1, 
                           data_mine_miss_restr_proc2$time, 
                           tvcs[1],
                           tvcs[2],
                           max(data_mine_miss_restr_proc2$time))
data_mine_miss_restr_proc2$less_90d_tr1_rec<-
create_time_varying_effect(data_mine_miss_restr_proc2$less_90d_tr1, 
                           data_mine_miss_restr_proc2$time, 
                           tvcs[1],
                           tvcs[2],
                           max(data_mine_miss_restr_proc2$time))
  
  model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_alt<-
    cph(as.formula(paste0("Surv(lag_time,time,event==1)~ 
              cluster(id)+ 
              lag_tr_outcome +
              log_lag_dias_treat_imp_sin_na +
              lag_less_90d_tr1_rec+
              lag_comp_bpsc_y3_severe + 
              lag_policonsumo2 + 
              edad_al_ing_1 + 
              ano_nac_corr + 
              susinidum_oh +
              susinidum_coc +
              susinidum_pbc +
              susinidum_mar +
              psycom_dum_with +
              psycom_dum_study + 
              freq_cons_dum_5day +
              cond_oc_dum_2inact +
              cond_oc_dum_3unemp +
              susprindum_oh +
              susprindum_coc +
              susprindum_pbc +
              susprindum_mar+
              strat(tipo_de_plan_2_mod)")), 
        data=data_mine_miss_restr_proc2 %>% 
          data.table::as.data.table() %>% data.frame(), 
        x=TRUE, y=TRUE, surv=TRUE, iter.max = 250*4, tol = 1e-6)
  
  cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_alt)
  invisible("")
  print(paste0("iteration: ",i))
  print(tvcs)
  print(format(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_alt)$table[nrow(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_alt)$table), 1]))
  
}
[1] "iteration: 0.1"
[1] 2 8
[1] "1710.118"
[1] "iteration: 0.15"
[1]  3 12
[1] "1642.617"
[1] "iteration: 0.2"
[1]  4 16
[1] "1319.002"
[1] "iteration: 0.25"
[1]  5 20
[1] "885.2445"
[1] "iteration: 0.3"
[1]  6 24
[1] "503.2978"
[1] "iteration: 0.35"
[1]  7 28
[1] "363.183"
[1] "iteration: 0.4"
[1]  8 32
[1] "609.1047"
[1] "iteration: 0.45"
[1]  9 36
[1] "1241.478"
[1] "iteration: 0.5"
[1] 10 40
[1] "2116.105"
[1] "iteration: 0.55"
[1] 11 44
[1] "3014.795"
[1] "iteration: 0.6"
[1] 12 48
[1] "3752.182"
[1] "iteration: 0.65"
[1] 13 52
[1] "4241.554"
[1] "iteration: 0.7"
[1] 14 56
[1] "4487.798"
[1] "iteration: 0.75"
[1] 15 60
[1] "4534.477"
[1] "iteration: 0.8"
[1] 16 64
[1] "4426.349"
[1] "iteration: 0.85"
[1] 17 68
[1] "4208.187"
[1] "iteration: 0.9"
[1] 18 72
[1] "3908.978"
[1] "iteration: 0.95"
[1] 19 76
[1] "3553.854"
[1] "iteration: 1"
[1] 20 80
[1] "3162.509"
[1] "iteration: 1.05"
[1] 21 84
[1] "2756.184"
[1] "iteration: 1.1"
[1] 22 88
[1] "2339.299"
[1] "iteration: 1.15"
[1] 23 92
[1] "1922.443"
[1] "iteration: 1.2"
[1] 24 96
[1] "1526.312"
[1] "iteration: 1.25"
[1]  25 100
[1] "1186.425"
[1] "iteration: 1.3"
[1]  26 104
[1] "943.2429"
[1] "iteration: 1.35"
[1]  27 108
[1] "826.6724"
[1] "iteration: 1.4"
[1]  28 112
[1] "871.2033"
[1] "iteration: 1.45"
[1]  29 116
[1] "1124.667"
[1] "iteration: 1.5"
[1]  30 120
[1] "1270.214"
Code
# [1] "iteration: 0.35"
# [1]  7 28
# [1] "361.9089"

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
# [1] "iteration: 0.5"
# [1] 2.5 5.0 7.5
# [1] "475.9965"
change_points <- c(2.5, 5, 7.5)  # Example change points (months)
coefficients <- c(0.5, 1.5, 0.5, 1)  # Example coefficients
# Add the time-varying coefficient to your model
data_mine_miss_restr_proc2$lag_less_90d_tr1_rec2 <- with(data_mine_miss_restr_proc2,
                                                         lag_less_90d_tr1*time_varying_coeff(lag_time, change_points, coefficients))
data_mine_miss_restr_proc2$less_90d_tr1_rec2 <- with(data_mine_miss_restr_proc2,
                                                     less_90d_tr1*time_varying_coeff(lag_time, change_points, coefficients))

model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d<-
cph(Surv(lag_time,time,event)~ 
    cluster(id) + 
    lag_tr_outcome +
    log_lag_dias_treat_imp_sin_na +
    lag_less_90d_tr1_rec2 +
    lag_comp_bpsc_y3_severe + 
    lag_policonsumo2 + 
    edad_al_ing_1 + 
    ano_nac_corr + 
    susinidum_oh +
    susinidum_coc +
    susinidum_pbc +
    susinidum_mar +
    psycom_dum_with +
    psycom_dum_study + 
    freq_cons_dum_5day +
    cond_oc_dum_2inact +
    cond_oc_dum_3unemp +
    susprindum_oh +
    susprindum_coc +
    susprindum_pbc +
    susprindum_mar+
    strat(tipo_de_plan_2_mod), 
    #data= data_clean,
    data= data_mine_miss_restr_proc2 %>% dplyr::mutate(log_lag_dias_treat_imp_sin_na_rec= bs(log_lag_dias_treat_imp_sin_na, 3)) %>% data.table::as.data.table() %>% data.frame(),
    x=TRUE, y=TRUE, surv=TRUE, iter.max = 250*4, tol = 1e-6)

cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d)
                                chisq df       p
lag_tr_outcome                 54.041  1 2.0e-13
log_lag_dias_treat_imp_sin_na 144.161  1 < 2e-16
lag_less_90d_tr1_rec2          53.529  1 2.5e-13
lag_comp_bpsc_y3_severe        21.026  1 4.5e-06
lag_policonsumo2                8.345  1 0.00387
edad_al_ing_1                  33.844  1 6.0e-09
ano_nac_corr                    8.075  1 0.00449
susinidum_oh                   40.358  1 2.1e-10
susinidum_coc                   0.976  1 0.32311
susinidum_pbc                   6.830  1 0.00897
susinidum_mar                  25.019  1 5.7e-07
psycom_dum_with                17.112  1 3.5e-05
psycom_dum_study               40.295  1 2.2e-10
freq_cons_dum_5day              2.353  1 0.12505
cond_oc_dum_2inact              3.057  1 0.08040
cond_oc_dum_3unemp             14.775  1 0.00012
susprindum_oh                  25.139  1 5.3e-07
susprindum_coc                  0.780  1 0.37706
susprindum_pbc                 20.331  1 6.5e-06
susprindum_mar                  2.509  1 0.11319
GLOBAL                        477.463 20 < 2e-16
Code
message(paste0("The model improved from ", round(cox.zph(cox_iiw_proposed)$table[nrow(cox.zph(cox_iiw_proposed)$table), 1],1)," to 476"))

The model improved from 495.5 to 476

Code
#The model improved from 495.5 to 476


#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
# [1] "iteration: 0.35"
# [1]  7 28
# [1] "361.9089"

tvcs <- c(20,80)*0.35

data_mine_miss_restr_proc2$lag_less_90d_tr1_rec<-
create_time_varying_effect(data_mine_miss_restr_proc2$lag_less_90d_tr1, 
                           data_mine_miss_restr_proc2$time, 
                           tvcs[1],
                           tvcs[2],
                           max(data_mine_miss_restr_proc2$time))
data_mine_miss_restr_proc2$less_90d_tr1_rec<-
create_time_varying_effect(data_mine_miss_restr_proc2$less_90d_tr1, 
                           data_mine_miss_restr_proc2$time, 
                           tvcs[1],
                           tvcs[2],
                           max(data_mine_miss_restr_proc2$time))
  
  model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_alt<-
    cph(as.formula(paste0("Surv(lag_time,time,event==1)~ 
              cluster(id)+ 
              lag_tr_outcome +
              log_lag_dias_treat_imp_sin_na +
              lag_less_90d_tr1_rec+
              lag_comp_bpsc_y3_severe + 
              lag_policonsumo2 + 
              edad_al_ing_1 + 
              ano_nac_corr + 
              susinidum_oh +
              susinidum_coc +
              susinidum_pbc +
              susinidum_mar +
              psycom_dum_with +
              psycom_dum_study + 
              freq_cons_dum_5day +
              cond_oc_dum_2inact +
              cond_oc_dum_3unemp +
              susprindum_oh +
              susprindum_coc +
              susprindum_pbc +
              susprindum_mar+
              strat(tipo_de_plan_2_mod)")), 
        data=data_mine_miss_restr_proc2 %>% 
          data.table::as.data.table() %>% data.frame(), 
        x=TRUE, y=TRUE, surv=TRUE, iter.max = 250*4, tol = 1e-6)
  
cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_alt)
                                chisq df       p
lag_tr_outcome                 20.783  1 5.1e-06
log_lag_dias_treat_imp_sin_na  41.447  1 1.2e-10
lag_less_90d_tr1_rec           14.941  1 0.00011
lag_comp_bpsc_y3_severe        16.927  1 3.9e-05
lag_policonsumo2                8.209  1 0.00417
edad_al_ing_1                  20.696  1 5.4e-06
ano_nac_corr                    2.130  1 0.14448
susinidum_oh                   33.052  1 9.0e-09
susinidum_coc                   1.324  1 0.24985
susinidum_pbc                   3.595  1 0.05794
susinidum_mar                  22.314  1 2.3e-06
psycom_dum_with                 6.947  1 0.00840
psycom_dum_study               11.181  1 0.00083
freq_cons_dum_5day              1.635  1 0.20096
cond_oc_dum_2inact              3.157  1 0.07561
cond_oc_dum_3unemp             12.212  1 0.00047
susprindum_oh                  19.510  1 1.0e-05
susprindum_coc                  0.602  1 0.43788
susprindum_pbc                 13.440  1 0.00025
susprindum_mar                  4.138  1 0.04192
GLOBAL                        363.183 20 < 2e-16
Code
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
invisible("Analyse the violation of proportional hazards")
par(mar = c(5, 4, 4, 5))
par(mfrow=c(3,1))
res240422_2 <- resid(model_after_mod_not_log_lag_dias_treat_lag_less_90d, "scaledsch")
time240422_2 <- as.numeric(dimnames(res240422_2)[[1]])

z_240422_2_1 <- loess(res240422_2[,"log_lag_dias_treat_imp_sin_na"] ~ time240422_2, span=0.50)   # residuals 
# x11() 
plot(time240422_2, fitted(z_240422_2_1))
lines(supsmu(time240422_2, res240422_2[,"log_lag_dias_treat_imp_sin_na"]),lty=2)
title("Sch. res., log_lag_dias_treat_imp_sin_na")

z_240422_2_2 <- loess(res240422_2[,"lag_comp_bpsc_y3_severe"] ~ time240422_2, span=0.50)   #
plot(time240422_2, fitted(z_240422_2_2))
lines(supsmu(time240422_2, res240422_2[,"lag_comp_bpsc_y3_severe"]),lty=2)
title("Sch. res., lag_comp_bpsc_y3_severe")

km_lag_comp_bpsc_y3_severe <- survfit(Surv(lag_time,time,event)~ lag_comp_bpsc_y3_severe+
    cluster(id), data = data_mine_miss_proc2)
#autoplot(km_home) # just to see the km curves

plot(km_lag_comp_bpsc_y3_severe, fun = "cloglog", xlab = "Time (in days) using log",col=c("black", "red"),
     ylab = "log-log survival", main = "log-log curves by km_lag_comp_bpsc_y3_severe")
legend("bottomright", legend=c("=0", "=1"), col=c("black", "red"), lty=1)

Code
recorded_plot <- recordPlot() 
folder_path <- ifelse(dir.exists("E:/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/"),
                      "E:/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/",
                      "C:/Users/CISS Fondecyt/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/")
png(paste0(folder_path,"zph/proportionallity_comp_bpsc.png"), height=13, width=10, res=500, units="in") 
recorded_plot
dev.off()
png 
  2 
Code
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
invisible("2024-04-30: Analyse the violation of proportional hazards (restricted)")
par(mar = c(5, 4, 4, 5))
par(mfrow=c(3,1))
res240422_2_restr <- resid(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_alt, "scaledsch")
time240422_2_restr <- as.numeric(dimnames(res240422_2_restr)[[1]])

z_240422_2_1_restr <- loess(res240422_2_restr[,"log_lag_dias_treat_imp_sin_na"] ~ time240422_2_restr, span=0.50)   # residuals 
# x11() 
plot(time240422_2_restr, fitted(z_240422_2_1_restr))
lines(supsmu(time240422_2_restr, res240422_2_restr[,"log_lag_dias_treat_imp_sin_na"]),lty=2)
title("Sch. res., log_lag_dias_treat_imp_sin_na (restricted)")

z_240422_2_2_restr <- loess(res240422_2_restr[,"lag_comp_bpsc_y3_severe"] ~ time240422_2_restr, span=0.50)   #
plot(time240422_2_restr, fitted(z_240422_2_2_restr))
lines(supsmu(time240422_2_restr, res240422_2_restr[,"lag_comp_bpsc_y3_severe"]),lty=2)
title("Sch. res., lag_comp_bpsc_y3_severe (restricted)")

km_lag_comp_bpsc_y3_severe_restr <- survfit(Surv(lag_time,time,event)~ lag_comp_bpsc_y3_severe+
    cluster(id), data = data_mine_miss_restr_proc2)
#autoplot(km_home) # just to see the km curves

plot(km_lag_comp_bpsc_y3_severe_restr, fun = "cloglog", xlab = "Time (in days) using log",col=c("black", "red"),
     ylab = "log-log survival", main = "log-log curves by km_lag_comp_bpsc_y3_severe")
legend("bottomright", legend=c("=0", "=1"), col=c("black", "red"), lty=1)

Code
recorded_plot <- recordPlot() 
folder_path <- ifelse(dir.exists("E:/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/"),
                      "E:/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/",
                      "C:/Users/CISS Fondecyt/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/")
png(paste0(folder_path,"zph/proportionallity_comp_bpsc_restr.png"), height=13, width=10, res=500, units="in") 
recorded_plot
dev.off()
png 
  2 
Code
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
invisible("2024-05-30: restricted w/ tipo de plan 2 mod; find TVCs")

message(paste0("The model was better after correction: ",
               round(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_alt)$table[nrow(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_alt)$table), 1],1)
               ," vs. the original: ",
               round(cox.zph(cox_iiw_proposed)$table[nrow(cox.zph(cox_iiw_proposed)$table), 1],1)
               ))

The model was better after correction: 363.2 vs. the original: 495.5

Code
#The model was better after correction: 361.9 vs. the original: 495.5
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

for (i in seq(.1,7,.1)) {
  change_points <- c(5*i, 10*i, 15*i)  # Example change points (months)
  coefficients <- c(0.5, 1.5, 0.5, 1)  # Example coefficients
  # Add the time-varying coefficient to your model
data_mine_miss_restr_proc2$lag_comp_bpsc_y3_severe_rec2 <- with(data_mine_miss_restr_proc2, lag_comp_bpsc_y3_severe*time_varying_coeff(lag_time, change_points, coefficients))

data_mine_miss_restr_proc2$comp_bpsc_y3_severe_rec2 <- with(data_mine_miss_restr_proc2, comp_bpsc_y3_severe*time_varying_coeff(time, change_points, coefficients))
  
  model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe<-
    cph(as.formula(paste0("Surv(lag_time,time,event==1)~ 
              cluster(id)+ 
              lag_tr_outcome +
              log_lag_dias_treat_imp_sin_na +
              lag_less_90d_tr1_rec+
              lag_comp_bpsc_y3_severe_rec2 + 
              lag_policonsumo2 + 
              edad_al_ing_1 + 
              ano_nac_corr + 
              susinidum_oh +
              susinidum_coc +
              susinidum_pbc +
              susinidum_mar +
              psycom_dum_with +
              psycom_dum_study + 
              freq_cons_dum_5day +
              cond_oc_dum_2inact +
              cond_oc_dum_3unemp +
              susprindum_oh +
              susprindum_coc +
              susprindum_pbc +
              susprindum_mar+
              strat(tipo_de_plan_2_mod)")), 
            data=data_mine_miss_restr_proc2 %>% 
          data.table::as.data.table() %>% data.frame(), 
        x=TRUE, y=TRUE, surv=TRUE, iter.max = 250*4, tol = 1e-6)
  
  cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe)
  invisible("")
  print(paste0("iteration: ",i))
  print(change_points)
  print(format(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe)$table[nrow(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe)$table), 1]))
  
}
[1] "iteration: 0.1"
[1] 0.5 1.0 1.5
[1] "366.7352"
[1] "iteration: 0.2"
[1] 1 2 3
[1] "367.6181"
[1] "iteration: 0.3"
[1] 1.5 3.0 4.5
[1] "359.0755"
[1] "iteration: 0.4"
[1] 2 4 6
[1] "358.9647"
[1] "iteration: 0.5"
[1] 2.5 5.0 7.5
[1] "362.0432"
[1] "iteration: 0.6"
[1] 3 6 9
[1] "367.2566"
[1] "iteration: 0.7"
[1]  3.5  7.0 10.5
[1] "368.7599"
[1] "iteration: 0.8"
[1]  4  8 12
[1] "364.0977"
[1] "iteration: 0.9"
[1]  4.5  9.0 13.5
[1] "363.4215"
[1] "iteration: 1"
[1]  5 10 15
[1] "361.3236"
[1] "iteration: 1.1"
[1]  5.5 11.0 16.5
[1] "361.5829"
[1] "iteration: 1.2"
[1]  6 12 18
[1] "359.3325"
[1] "iteration: 1.3"
[1]  6.5 13.0 19.5
[1] "357.4246"
[1] "iteration: 1.4"
[1]  7 14 21
[1] "356.7213"
[1] "iteration: 1.5"
[1]  7.5 15.0 22.5
[1] "356.5751"
[1] "iteration: 1.6"
[1]  8 16 24
[1] "356.1985"
[1] "iteration: 1.7"
[1]  8.5 17.0 25.5
[1] "355.9476"
[1] "iteration: 1.8"
[1]  9 18 27
[1] "356.0388"
[1] "iteration: 1.9"
[1]  9.5 19.0 28.5
[1] "355.7839"
[1] "iteration: 2"
[1] 10 20 30
[1] "355.8856"
[1] "iteration: 2.1"
[1] 10.5 21.0 31.5
[1] "355.274"
[1] "iteration: 2.2"
[1] 11 22 33
[1] "355.3662"
[1] "iteration: 2.3"
[1] 11.5 23.0 34.5
[1] "355.2717"
[1] "iteration: 2.4"
[1] 12 24 36
[1] "355.2632"
[1] "iteration: 2.5"
[1] 12.5 25.0 37.5
[1] "355.6316"
[1] "iteration: 2.6"
[1] 13 26 39
[1] "355.7911"
[1] "iteration: 2.7"
[1] 13.5 27.0 40.5
[1] "355.9782"
[1] "iteration: 2.8"
[1] 14 28 42
[1] "355.725"
[1] "iteration: 2.9"
[1] 14.5 29.0 43.5
[1] "356.2413"
[1] "iteration: 3"
[1] 15 30 45
[1] "356.405"
[1] "iteration: 3.1"
[1] 15.5 31.0 46.5
[1] "356.2095"
[1] "iteration: 3.2"
[1] 16 32 48
[1] "357.5944"
[1] "iteration: 3.3"
[1] 16.5 33.0 49.5
[1] "358.5072"
[1] "iteration: 3.4"
[1] 17 34 51
[1] "359.3226"
[1] "iteration: 3.5"
[1] 17.5 35.0 52.5
[1] "359.6748"
[1] "iteration: 3.6"
[1] 18 36 54
[1] "359.214"
[1] "iteration: 3.7"
[1] 18.5 37.0 55.5
[1] "359.4672"
[1] "iteration: 3.8"
[1] 19 38 57
[1] "361.2473"
[1] "iteration: 3.9"
[1] 19.5 39.0 58.5
[1] "361.1348"
[1] "iteration: 4"
[1] 20 40 60
[1] "361.7292"
[1] "iteration: 4.1"
[1] 20.5 41.0 61.5
[1] "364.4573"
[1] "iteration: 4.2"
[1] 21 42 63
[1] "363.8848"
[1] "iteration: 4.3"
[1] 21.5 43.0 64.5
[1] "363.5723"
[1] "iteration: 4.4"
[1] 22 44 66
[1] "364.5945"
[1] "iteration: 4.5"
[1] 22.5 45.0 67.5
[1] "365.1071"
[1] "iteration: 4.6"
[1] 23 46 69
[1] "365.8701"
[1] "iteration: 4.7"
[1] 23.5 47.0 70.5
[1] "364.955"
[1] "iteration: 4.8"
[1] 24 48 72
[1] "365.3112"
[1] "iteration: 4.9"
[1] 24.5 49.0 73.5
[1] "365.706"
[1] "iteration: 5"
[1] 25 50 75
[1] "366.3371"
[1] "iteration: 5.1"
[1] 25.5 51.0 76.5
[1] "367.0493"
[1] "iteration: 5.2"
[1] 26 52 78
[1] "367.9306"
[1] "iteration: 5.3"
[1] 26.5 53.0 79.5
[1] "368.4692"
[1] "iteration: 5.4"
[1] 27 54 81
[1] "368.9484"
[1] "iteration: 5.5"
[1] 27.5 55.0 82.5
[1] "370.2654"
[1] "iteration: 5.6"
[1] 28 56 84
[1] "370.4634"
[1] "iteration: 5.7"
[1] 28.5 57.0 85.5
[1] "371.0471"
[1] "iteration: 5.8"
[1] 29 58 87
[1] "371.8349"
[1] "iteration: 5.9"
[1] 29.5 59.0 88.5
[1] "372.2448"
[1] "iteration: 6"
[1] 30 60 90
[1] "371.6"
[1] "iteration: 6.1"
[1] 30.5 61.0 91.5
[1] "371.2923"
[1] "iteration: 6.2"
[1] 31 62 93
[1] "371.9248"
[1] "iteration: 6.3"
[1] 31.5 63.0 94.5
[1] "372.2759"
[1] "iteration: 6.4"
[1] 32 64 96
[1] "371.9611"
[1] "iteration: 6.5"
[1] 32.5 65.0 97.5
[1] "371.9074"
[1] "iteration: 6.6"
[1] 33 66 99
[1] "371.3031"
[1] "iteration: 6.7"
[1]  33.5  67.0 100.5
[1] "371.2377"
[1] "iteration: 6.8"
[1]  34  68 102
[1] "370.6362"
[1] "iteration: 6.9"
[1]  34.5  69.0 103.5
[1] "371.8994"
[1] "iteration: 7"
[1]  35  70 105
[1] "371.8839"
Code
message(paste0("The previously modified model: ",
               round(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_alt)$table[nrow(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_alt)$table), 1],1),
               " vs. the actual models: 354.7"
               ))

The previously modified model: 363.2 vs. the actual models: 354.7

Code
#The previously modified model: 361.9 vs. the actual models: 354.7

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
results_bpsc_severe <- data.frame(spec = character(), aic = numeric(), cox_zph = numeric(), success = logical())

for (i in seq(.1,1.5,.025)) {
tvcs <- c(20,80)*i

data_mine_miss_restr_proc2$lag_comp_bpsc_y3_severe_rec<-
create_time_varying_effect(data_mine_miss_restr_proc2$lag_comp_bpsc_y3_severe, 
                   data_mine_miss_restr_proc2$time, 
                   tvcs[1],
                   tvcs[2],
                   max(data_mine_miss_restr_proc2$time))
data_mine_miss_restr_proc2$comp_bpsc_y3_severe_rec<-
create_time_varying_effect(data_mine_miss_restr_proc2$comp_bpsc_y3_severe, 
                   data_mine_miss_restr_proc2$time, 
                   tvcs[1],
                   tvcs[2],
                   max(data_mine_miss_restr_proc2$time))
  
  model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt<-
    cph(as.formula(paste0("Surv(lag_time,time,event==1)~ 
              cluster(id)+ 
              lag_tr_outcome +
              log_lag_dias_treat_imp_sin_na +
              lag_less_90d_tr1_rec+
              lag_comp_bpsc_y3_severe_rec + 
              lag_policonsumo2 + 
              edad_al_ing_1 + 
              ano_nac_corr + 
              susinidum_oh +
              susinidum_coc +
              susinidum_pbc +
              susinidum_mar +
              psycom_dum_with +
              psycom_dum_study + 
              freq_cons_dum_5day +
              cond_oc_dum_2inact +
              cond_oc_dum_3unemp +
              susprindum_oh +
              susprindum_coc +
              susprindum_pbc +
              susprindum_mar+
              strat(tipo_de_plan_2_mod)")), 
        data=data_mine_miss_restr_proc2 %>% 
          data.table::as.data.table() %>% data.frame(), 
        x=TRUE, y=TRUE, surv=TRUE, iter.max = 250*4, tol = 1e-6)
  
  cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt)
  invisible("")
  print(paste0("iteration: ",i))
  aic_iter <- round(extractAIC(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt)[[2]],1)
  print( paste0("Time points: ",tvcs[1],", ", tvcs[2]))
  print( paste0("AIC: ", aic_iter))
  cox_zph_iter <- format(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt)$table[nrow(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt)$table), 1])
  print(paste0("Cox ZPH: ", cox_zph_iter))
  
  results_bpsc_severe <- rbind.data.frame(results_bpsc_severe, 
                                          cbind.data.frame(
                                            spec = paste0("iteration: ",i, "; tvcs: ", tvcs[1],", ", tvcs[2]), 
                                            aic = aic_iter,
                                            cox_zph = cox_zph_iter,
                                            success = T
                                          ))
}
[1] "iteration: 0.1"
[1] "Time points: 2, 8"
[1] "AIC: 244648.5"
[1] "Cox ZPH: 2027.052"
[1] "iteration: 0.125"
[1] "Time points: 2.5, 10"
[1] "AIC: 244584.1"
[1] "Cox ZPH: 2126.697"
[1] "iteration: 0.15"
[1] "Time points: 3, 12"
[1] "AIC: 244529.8"
[1] "Cox ZPH: 2176.088"
[1] "iteration: 0.175"
[1] "Time points: 3.5, 14"
[1] "AIC: 244488.4"
[1] "Cox ZPH: 2165.634"
[1] "iteration: 0.2"
[1] "Time points: 4, 16"
[1] "AIC: 244463.4"
[1] "Cox ZPH: 2084.661"
[1] "iteration: 0.225"
[1] "Time points: 4.5, 18"
[1] "AIC: 244450.7"
[1] "Cox ZPH: 1955.693"
[1] "iteration: 0.25"
[1] "Time points: 5, 20"
[1] "AIC: 244448.3"
[1] "Cox ZPH: 1792.717"
[1] "iteration: 0.275"
[1] "Time points: 5.5, 22"
[1] "AIC: 244464.3"
[1] "Cox ZPH: 1576.648"
[1] "iteration: 0.3"
[1] "Time points: 6, 24"
[1] "AIC: 244495.7"
[1] "Cox ZPH: 1325.06"
[1] "iteration: 0.325"
[1] "Time points: 6.5, 26"
[1] "AIC: 244537.3"
[1] "Cox ZPH: 1069.782"
[1] "iteration: 0.35"
[1] "Time points: 7, 28"
[1] "AIC: 244590.8"
[1] "Cox ZPH: 825.2673"
[1] "iteration: 0.375"
[1] "Time points: 7.5, 30"
[1] "AIC: 244656"
[1] "Cox ZPH: 608.8866"
[1] "iteration: 0.4"
[1] "Time points: 8, 32"
[1] "AIC: 244730.5"
[1] "Cox ZPH: 444.7581"
[1] "iteration: 0.425"
[1] "Time points: 8.5, 34"
[1] "AIC: 244815.5"
[1] "Cox ZPH: 352.1245"
[1] "iteration: 0.45"
[1] "Time points: 9, 36"
[1] "AIC: 244909.7"
[1] "Cox ZPH: 353.8899"
[1] "iteration: 0.475"
[1] "Time points: 9.5, 38"
[1] "AIC: 245009.8"
[1] "Cox ZPH: 465.3513"
[1] "iteration: 0.5"
[1] "Time points: 10, 40"
[1] "AIC: 245111.2"
[1] "Cox ZPH: 687.6218"
[1] "iteration: 0.525"
[1] "Time points: 10.5, 42"
[1] "AIC: 245215"
[1] "Cox ZPH: 1018.352"
[1] "iteration: 0.55"
[1] "Time points: 11, 44"
[1] "AIC: 245321.5"
[1] "Cox ZPH: 1450.489"
[1] "iteration: 0.575"
[1] "Time points: 11.5, 46"
[1] "AIC: 245426.3"
[1] "Cox ZPH: 1956.628"
[1] "iteration: 0.6"
[1] "Time points: 12, 48"
[1] "AIC: 245526.3"
[1] "Cox ZPH: 2500.774"
[1] "iteration: 0.625"
[1] "Time points: 12.5, 50"
[1] "AIC: 245620.4"
[1] "Cox ZPH: 3058.768"
[1] "iteration: 0.65"
[1] "Time points: 13, 52"
[1] "AIC: 245707.3"
[1] "Cox ZPH: 3601.197"
[1] "iteration: 0.675"
[1] "Time points: 13.5, 54"
[1] "AIC: 245785.6"
[1] "Cox ZPH: 4103.462"
[1] "iteration: 0.7"
[1] "Time points: 14, 56"
[1] "AIC: 245853"
[1] "Cox ZPH: 4540.246"
[1] "iteration: 0.725"
[1] "Time points: 14.5, 58"
[1] "AIC: 245907.2"
[1] "Cox ZPH: 4898.792"
[1] "iteration: 0.75"
[1] "Time points: 15, 60"
[1] "AIC: 245946"
[1] "Cox ZPH: 5174.502"
[1] "iteration: 0.775"
[1] "Time points: 15.5, 62"
[1] "AIC: 245968.4"
[1] "Cox ZPH: 5365.95"
[1] "iteration: 0.8"
[1] "Time points: 16, 64"
[1] "AIC: 245972.6"
[1] "Cox ZPH: 5481.321"
[1] "iteration: 0.825"
[1] "Time points: 16.5, 66"
[1] "AIC: 245957.5"
[1] "Cox ZPH: 5527.575"
[1] "iteration: 0.85"
[1] "Time points: 17, 68"
[1] "AIC: 245922.4"
[1] "Cox ZPH: 5514.13"
[1] "iteration: 0.875"
[1] "Time points: 17.5, 70"
[1] "AIC: 245866.7"
[1] "Cox ZPH: 5451.26"
[1] "iteration: 0.9"
[1] "Time points: 18, 72"
[1] "AIC: 245789.7"
[1] "Cox ZPH: 5347.218"
[1] "iteration: 0.925"
[1] "Time points: 18.5, 74"
[1] "AIC: 245690.6"
[1] "Cox ZPH: 5203.803"
[1] "iteration: 0.95"
[1] "Time points: 19, 76"
[1] "AIC: 245569.6"
[1] "Cox ZPH: 5028.824"
[1] "iteration: 0.975"
[1] "Time points: 19.5, 78"
[1] "AIC: 245425.6"
[1] "Cox ZPH: 4827.561"
[1] "iteration: 1"
[1] "Time points: 20, 80"
[1] "AIC: 245258.7"
[1] "Cox ZPH: 4603.399"
[1] "iteration: 1.025"
[1] "Time points: 20.5, 82"
[1] "AIC: 245070.3"
[1] "Cox ZPH: 4363.59"
[1] "iteration: 1.05"
[1] "Time points: 21, 84"
[1] "AIC: 244861.8"
[1] "Cox ZPH: 4111.058"
[1] "iteration: 1.075"
[1] "Time points: 21.5, 86"
[1] "AIC: 244633.9"
[1] "Cox ZPH: 3848.298"
[1] "iteration: 1.1"
[1] "Time points: 22, 88"
[1] "AIC: 244389.1"
[1] "Cox ZPH: 3579.744"
[1] "iteration: 1.125"
[1] "Time points: 22.5, 90"
[1] "AIC: 244129.8"
[1] "Cox ZPH: 3308.287"
[1] "iteration: 1.15"
[1] "Time points: 23, 92"
[1] "AIC: 243857.6"
[1] "Cox ZPH: 3037.355"
[1] "iteration: 1.175"
[1] "Time points: 23.5, 94"
[1] "AIC: 243578.2"
[1] "Cox ZPH: 2772.307"
[1] "iteration: 1.2"
[1] "Time points: 24, 96"
[1] "AIC: 243291.8"
[1] "Cox ZPH: 2512.812"
[1] "iteration: 1.225"
[1] "Time points: 24.5, 98"
[1] "AIC: 243000.5"
[1] "Cox ZPH: 2265.902"
[1] "iteration: 1.25"
[1] "Time points: 25, 100"
[1] "AIC: 242705.8"
[1] "Cox ZPH: 2025.213"
[1] "iteration: 1.275"
[1] "Time points: 25.5, 102"
[1] "AIC: 242407"
[1] "Cox ZPH: 1795.87"
[1] "iteration: 1.3"
[1] "Time points: 26, 104"
[1] "AIC: 242104.5"
[1] "Cox ZPH: 1579.576"
[1] "iteration: 1.325"
[1] "Time points: 26.5, 106"
[1] "AIC: 241802.2"
[1] "Cox ZPH: 1380.713"
[1] "iteration: 1.35"
[1] "Time points: 27, 108"
[1] "AIC: 241506.2"
[1] "Cox ZPH: 1203.313"
[1] "iteration: 1.375"
[1] "Time points: 27.5, 110"
[1] "AIC: 241224.7"
[1] "Cox ZPH: 1051.053"
[1] "iteration: 1.4"
[1] "Time points: 28, 112"
[1] "AIC: 240963.6"
[1] "Cox ZPH: 927.1896"
[1] "iteration: 1.425"
[1] "Time points: 28.5, 114"
[1] "AIC: 240715"
[1] "Cox ZPH: 832.7559"
[1] "iteration: 1.45"
[1] "Time points: 29, 116"
[1] "AIC: 240498.4"
[1] "Cox ZPH: 775.9883"
[1] "iteration: 1.475"
[1] "Time points: 29.5, 118"
[1] "AIC: 240307.3"
[1] "Cox ZPH: 769.6182"
[1] "iteration: 1.5"
[1] "Time points: 30, 120"
[1] "AIC: 240155.7"
[1] "Cox ZPH: 837.3566"
Code
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

results_bpsc_severe[which(as.numeric(results_bpsc_severe$cox_zph)==
                            min(as.numeric(results_bpsc_severe$cox_zph))),]
                              spec      aic  cox_zph success
14 iteration: 0.425; tvcs: 8.5, 34 244815.5 352.1245    TRUE
Code
#                               spec      aic  cox_zph success
# 14 iteration: 0.425; tvcs: 8.5, 34 244802.6 351.5909    TRUE
tvcs <- c(20,80)*0.425

data_mine_miss_restr_proc2$lag_comp_bpsc_y3_severe_rec<-
create_time_varying_effect(data_mine_miss_restr_proc2$lag_comp_bpsc_y3_severe, 
                   data_mine_miss_restr_proc2$time, 
                   tvcs[1],
                   tvcs[2],
                   max(data_mine_miss_restr_proc2$time))
data_mine_miss_restr_proc2$comp_bpsc_y3_severe_rec<-
create_time_varying_effect(data_mine_miss_restr_proc2$comp_bpsc_y3_severe, 
                   data_mine_miss_restr_proc2$time, 
                   tvcs[1],
                   tvcs[2],
                   max(data_mine_miss_restr_proc2$time))


model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt<-
  cph(Surv(lag_time,time,event==1)~ 
            cluster(id)+ 
            lag_tr_outcome +
            log_lag_dias_treat_imp_sin_na +
            lag_less_90d_tr1_rec+
            lag_comp_bpsc_y3_severe_rec + 
            lag_policonsumo2 + 
            edad_al_ing_1 + 
            ano_nac_corr + 
            susinidum_oh +
            susinidum_coc +
            susinidum_pbc +
            susinidum_mar +
            psycom_dum_with +
            psycom_dum_study + 
            freq_cons_dum_5day +
            cond_oc_dum_2inact +
            cond_oc_dum_3unemp +
            susprindum_oh +
            susprindum_coc +
            susprindum_pbc +
            susprindum_mar+
            strat(tipo_de_plan_2_mod), 
      data=data_mine_miss_restr_proc2 %>% 
        data.table::as.data.table() %>% data.frame(), 
      x=TRUE, y=TRUE, surv=TRUE, iter.max = 250*4, tol = 1e-6)

cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt)
                                 chisq df       p
lag_tr_outcome                 19.1955  1 1.2e-05
log_lag_dias_treat_imp_sin_na  49.0939  1 2.4e-12
lag_less_90d_tr1_rec           42.0858  1 8.7e-11
lag_comp_bpsc_y3_severe_rec     1.5266  1 0.21662
lag_policonsumo2                1.5932  1 0.20687
edad_al_ing_1                  17.6296  1 2.7e-05
ano_nac_corr                    1.6974  1 0.19263
susinidum_oh                   23.7232  1 1.1e-06
susinidum_coc                   3.6139  1 0.05730
susinidum_pbc                   2.7404  1 0.09784
susinidum_mar                  13.2315  1 0.00028
psycom_dum_with                14.6452  1 0.00013
psycom_dum_study                8.0507  1 0.00455
freq_cons_dum_5day              0.4958  1 0.48136
cond_oc_dum_2inact              7.9362  1 0.00485
cond_oc_dum_3unemp              5.2995  1 0.02133
susprindum_oh                  11.5928  1 0.00066
susprindum_coc                  0.0487  1 0.82530
susprindum_pbc                  2.8441  1 0.09171
susprindum_mar                  7.8036  1 0.00521
GLOBAL                        352.1245 20 < 2e-16
Code
#                                  chisq df       p
# lag_tr_outcome                 19.4440  1 1.0e-05 ###
# log_lag_dias_treat_imp_sin_na  48.7236  1 2.9e-12 ####
# lag_less_90d_tr1_rec           42.0653  1 8.8e-11 ####
# lag_comp_bpsc_y3_severe_rec     1.5369  1 0.21508 
# lag_policonsumo2                1.5765  1 0.20926
# edad_al_ing_1                  17.5785  1 2.8e-05 ###
# ano_nac_corr                    1.6876  1 0.19392
# susinidum_oh                   23.5183  1 1.2e-06 ###
# susinidum_coc                   3.6019  1 0.05771
# susinidum_pbc                   2.6388  1 0.10428
# susinidum_mar                  13.2146  1 0.00028 ##
# psycom_dum_with                14.7208  1 0.00012 #
# psycom_dum_study                8.0570  1 0.00453 #
# freq_cons_dum_5day              0.5748  1 0.44834
# cond_oc_dum_2inact              7.9683  1 0.00476
# cond_oc_dum_3unemp              5.0871  1 0.02410 #
# susprindum_oh                  11.4536  1 0.00071 ##
# susprindum_coc                  0.0549  1 0.81473
# susprindum_pbc                  2.7348  1 0.09818
# susprindum_mar                  7.8675  1 0.00503 ##
# GLOBAL                        351.5909 20 < 2e-16
  
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
invisible("Analyse the violation of proportional hazards")
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

invisible("2024-04-30: restricted")

res240422_3_restr <- resid(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt, "scaledsch")
time240422_3_restr <- as.numeric(dimnames(res240422_3_restr)[[1]])

z_240422_3_1_restr <- loess(res240422_3_restr[,"susinidum_oh"] ~ time240422_3_restr, span=0.50)   # residuals 
# x11() 

par(mfrow= c(3,2))
plot(time240422_3_restr, fitted(z_240422_3_1_restr))
lines(supsmu(time240422_3_restr, res240422_3_restr[,"susinidum_oh"]),lty=2)
title("Sch. res., susinidum_oh")

z_240422_3_2_restr <- loess(res240422_3_restr[,"susinidum_oh"] ~ time240422_3_restr, span=0.50)   #
plot(time240422_3_restr, fitted(z_240422_3_2_restr))
lines(supsmu(time240422_3_restr, res240422_3_restr[,"susinidum_oh"]),lty=2)
title("Sch. res., susinidum_oh")

plot(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt)[which(attr(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt$coefficients, "names") == "psycom_dum_study")], lwd=2, main= "Residuals of psycom_dum_study")
abline(0,0, col="red", lty=3, lwd=2)
abline(h=model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt$coefficients["psycom_dum_study"], col=3, lwd=2, lty= 3)
legend("bottomleft", legend= c("Reference line for the null effect", "Average hazard over time", "Time-varying hazard"), lty=c(3,2,1), col= c("red",3,1), lwd=2, bty = "n", bg = "transparent")

plot(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt)[which(attr(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d$coefficients, "names") == "psycom_dum_with")], lwd=2, main= "Residuals of psycom_dum_with")
abline(0,0, col="red", lty=3, lwd=2)
abline(h=model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt$coefficients["psycom_dum_with"], col=3, lwd=2, lty= 3)
legend("bottomleft", legend= c("Reference line for the null effect", "Average hazard over time", "Time-varying hazard"), lty=c(3,2,1), col= c("red",3,1), lwd=2, bty = "n", bg = "transparent")

#autoplot(km_home) # just to see the km curves

km_psycom_dum_study_restr <- survfit(Surv(lag_time,time,event)~ psycom_dum_study+ 
    cluster(id), data = data_mine_miss_restr_proc2)
plot(km_psycom_dum_study_restr, fun = "cloglog", xlab = "Time (in days) using log",col=c("black", "red"), ylab = "log-log survival", main = "log-log curves by psycom_dum_study (restricted)")
legend("bottomright", legend=c("=0", "=1"), col=c("black", "red"), lty=1, bty = "n", bg = "transparent")

km_psycom_dum_with_restr <- survfit(Surv(lag_time,time,event)~ psycom_dum_with+ 
    cluster(id), data = data_mine_miss_restr_proc2)
#autoplot(km_home) # just to see the km curves

plot(km_psycom_dum_with_restr, fun = "cloglog", xlab = "Time (in days) using log",col=c("black", "red"), ylab = "log-log survival", main = "log-log curves by km_psycom_dum_with (restricted)")
legend("bottomright", legend=c("=0", "=1"), col=c("black", "red"), lty=1, bty = "n", bg = "transparent")

Code
recorded_plot <- recordPlot() 

folder_path <- ifelse(dir.exists("E:/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/"),
                      "E:/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/",
                      "C:/Users/CISS Fondecyt/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/")
png(paste0(folder_path,"zph/proportionallity_psycom_restr.png"), height=13, width=10, res=500, units="in") 
recorded_plot
dev.off()
png 
  2 
Code
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
invisible("2024-05-01: restricted")

res240422_4_restr <- resid(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt, "scaledsch")
time240422_4_restr <- as.numeric(dimnames(res240422_3_restr)[[1]])

z_240422_4_1_restr <- loess(res240422_4_restr[,"lag_tr_outcome"] ~ time240422_4_restr, span=0.50)   # residuals 
# x11() 

par(mfrow= c(3,1))
plot(time240422_4_restr, fitted(z_240422_4_1_restr))
lines(supsmu(time240422_4_restr, res240422_4_restr[,"lag_tr_outcome"]),lty=2)
title("Sch. res., lag_tr_outcome")

plot(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt)[which(attr(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt$coefficients, "names") == "lag_tr_outcome")], lwd=2, main= "Residuals of lag_tr_outcome")
abline(0,0, col="red", lty=3, lwd=2)
abline(h=model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt$coefficients["lag_tr_outcome"], col=3, lwd=2, lty= 3)
legend("bottomleft", legend= c("Reference line for the null effect", "Average hazard over time", "Time-varying hazard"), lty=c(3,2,1), col= c("red",3,1), lwd=2, bty = "n", bg = "transparent")

#autoplot(km_home) # just to see the km curves

km_lag_tr_outcome_restr <- survfit(Surv(lag_time,time,event)~ lag_tr_outcome+ 
    cluster(id), data = data_mine_miss_restr_proc2)
plot(km_lag_tr_outcome_restr, fun = "cloglog", xlab = "Time (in days) using log",col=c("black", "red"), ylab = "log-log survival", main = "log-log curves by km_lag_tr_outcome (restricted)")
legend("bottomright", legend=c("=0", "=1"), col=c("black", "red"), lty=1, bty = "n", bg = "transparent")

Code
recorded_plot <- recordPlot() 

folder_path <- ifelse(dir.exists("E:/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/"),
                      "E:/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/",
                      "C:/Users/CISS Fondecyt/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/")
png(paste0(folder_path,"zph/proportionallity_lag_tr_outcome_restr.png"), height=13, width=10, res=500, units="in") 
recorded_plot
dev.off()
png 
  2 
Code
#load("E:/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/20240502_avance_iiw.RData")
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
results_lag_tr_outcome <- data.frame(spec = character(), aic = numeric(), cox_zph = numeric(), success = logical())

for (i in seq(.1,1.5,.025)) {
tvcs <- c(20,80)*i

data_mine_miss_restr_proc2$lag_tr_outcome_rec<-
create_time_varying_effect(data_mine_miss_restr_proc2$lag_tr_outcome, 
                   data_mine_miss_restr_proc2$time, 
                   tvcs[1],
                   tvcs[2],
                   max(data_mine_miss_restr_proc2$time))
data_mine_miss_restr_proc2$tr_outcome_rec<-
create_time_varying_effect(data_mine_miss_restr_proc2$tr_outcome, 
                   data_mine_miss_restr_proc2$time, 
                   tvcs[1],
                   tvcs[2],
                   max(data_mine_miss_restr_proc2$time))
  
  model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out<-
    cph(as.formula(paste0("Surv(lag_time,time,event==1)~ 
              cluster(id)+ 
              lag_tr_outcome_rec +
              log_lag_dias_treat_imp_sin_na +
              lag_less_90d_tr1_rec+
              lag_comp_bpsc_y3_severe_rec + 
              lag_policonsumo2 + 
              edad_al_ing_1 + 
              ano_nac_corr + 
              susinidum_oh +
              susinidum_coc +
              susinidum_pbc +
              susinidum_mar +
              psycom_dum_with +
              psycom_dum_study + 
              freq_cons_dum_5day +
              cond_oc_dum_2inact +
              cond_oc_dum_3unemp +
              susprindum_oh +
              susprindum_coc +
              susprindum_pbc +
              susprindum_mar+
              strat(tipo_de_plan_2_mod)")), 
        data=data_mine_miss_restr_proc2 %>% 
          data.table::as.data.table() %>% data.frame(), 
        x=TRUE, y=TRUE, surv=TRUE, iter.max = 250*4, tol = 1e-6)
  
  cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out)
  invisible("")
  print(paste0("iteration: ",i))
  aic_iter <- round(extractAIC(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out)[[2]],1)
  print( paste0("Time points: ",tvcs[1],", ", tvcs[2]))
  print( paste0("AIC: ", aic_iter))
  cox_zph_iter <- format(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out)$table[nrow(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out)$table), 1])
  print(paste0("Cox ZPH: ", cox_zph_iter))
  
  results_lag_tr_outcome <- rbind.data.frame(results_lag_tr_outcome, 
                                          cbind.data.frame(
                                            spec = paste0("iteration: ",i, "; tvcs: ", tvcs[1],", ", tvcs[2]), 
                                            aic = aic_iter,
                                            cox_zph = cox_zph_iter,
                                            success = T
                                          ))
}
[1] "iteration: 0.1"
[1] "Time points: 2, 8"
[1] "AIC: 241646.4"
[1] "Cox ZPH: 6034.712"
[1] "iteration: 0.125"
[1] "Time points: 2.5, 10"
[1] "AIC: 241490.1"
[1] "Cox ZPH: 6318.221"
[1] "iteration: 0.15"
[1] "Time points: 3, 12"
[1] "AIC: 241364.7"
[1] "Cox ZPH: 6459.451"
[1] "iteration: 0.175"
[1] "Time points: 3.5, 14"
[1] "AIC: 241275"
[1] "Cox ZPH: 6445.888"
[1] "iteration: 0.2"
[1] "Time points: 4, 16"
[1] "AIC: 241227.8"
[1] "Cox ZPH: 6262.837"
[1] "iteration: 0.225"
[1] "Time points: 4.5, 18"
[1] "AIC: 241218.5"
[1] "Cox ZPH: 5937.099"
[1] "iteration: 0.25"
[1] "Time points: 5, 20"
[1] "AIC: 241244"
[1] "Cox ZPH: 5480.285"
[1] "iteration: 0.275"
[1] "Time points: 5.5, 22"
[1] "AIC: 241314.3"
[1] "Cox ZPH: 4871.568"
[1] "iteration: 0.3"
[1] "Time points: 6, 24"
[1] "AIC: 241431.2"
[1] "Cox ZPH: 4108.697"
[1] "iteration: 0.325"
[1] "Time points: 6.5, 26"
[1] "AIC: 241579.4"
[1] "Cox ZPH: 3270.654"
[1] "iteration: 0.35"
[1] "Time points: 7, 28"
[1] "AIC: 241763.2"
[1] "Cox ZPH: 2385.785"
[1] "iteration: 0.375"
[1] "Time points: 7.5, 30"
[1] "AIC: 241978.3"
[1] "Cox ZPH: 1540.282"
[1] "iteration: 0.4"
[1] "Time points: 8, 32"
[1] "AIC: 242214.4"
[1] "Cox ZPH: 856.6484"
[1] "iteration: 0.425"
[1] "Time points: 8.5, 34"
[1] "AIC: 242469.5"
[1] "Cox ZPH: 423.423"
[1] "iteration: 0.45"
[1] "Time points: 9, 36"
[1] "AIC: 242741.2"
[1] "Cox ZPH: 321.6527"
[1] "iteration: 0.475"
[1] "Time points: 9.5, 38"
[1] "AIC: 243020.3"
[1] "Cox ZPH: 600.673"
[1] "iteration: 0.5"
[1] "Time points: 10, 40"
[1] "AIC: 243294.4"
[1] "Cox ZPH: 1241.962"
[1] "iteration: 0.525"
[1] "Time points: 10.5, 42"
[1] "AIC: 243564.4"
[1] "Cox ZPH: 2200.14"
[1] "iteration: 0.55"
[1] "Time points: 11, 44"
[1] "AIC: 243826.5"
[1] "Cox ZPH: 3401.45"
[1] "iteration: 0.575"
[1] "Time points: 11.5, 46"
[1] "AIC: 244069.7"
[1] "Cox ZPH: 4719.541"
[1] "iteration: 0.6"
[1] "Time points: 12, 48"
[1] "AIC: 244289.8"
[1] "Cox ZPH: 6050.557"
[1] "iteration: 0.625"
[1] "Time points: 12.5, 50"
[1] "AIC: 244485"
[1] "Cox ZPH: 7321.242"
[1] "iteration: 0.65"
[1] "Time points: 13, 52"
[1] "AIC: 244649.6"
[1] "Cox ZPH: 8448.845"
[1] "iteration: 0.675"
[1] "Time points: 13.5, 54"
[1] "AIC: 244779.6"
[1] "Cox ZPH: 9389.054"
[1] "iteration: 0.7"
[1] "Time points: 14, 56"
[1] "AIC: 244870"
[1] "Cox ZPH: 10113.72"
[1] "iteration: 0.725"
[1] "Time points: 14.5, 58"
[1] "AIC: 244916.8"
[1] "Cox ZPH: 10634.71"
[1] "iteration: 0.75"
[1] "Time points: 15, 60"
[1] "AIC: 244917.4"
[1] "Cox ZPH: 10977.17"
[1] "iteration: 0.775"
[1] "Time points: 15.5, 62"
[1] "AIC: 244869.9"
[1] "Cox ZPH: 11163.84"
[1] "iteration: 0.8"
[1] "Time points: 16, 64"
[1] "AIC: 244772.8"
[1] "Cox ZPH: 11235.97"
[1] "iteration: 0.825"
[1] "Time points: 16.5, 66"
[1] "AIC: 244626.8"
[1] "Cox ZPH: 11225.28"
[1] "iteration: 0.85"
[1] "Time points: 17, 68"
[1] "AIC: 244433.5"
[1] "Cox ZPH: 11152.15"
[1] "iteration: 0.875"
[1] "Time points: 17.5, 70"
[1] "AIC: 244193.4"
[1] "Cox ZPH: 11031.52"
[1] "iteration: 0.9"
[1] "Time points: 18, 72"
[1] "AIC: 243905.5"
[1] "Cox ZPH: 10877.46"
[1] "iteration: 0.925"
[1] "Time points: 18.5, 74"
[1] "AIC: 243572.4"
[1] "Cox ZPH: 10696.07"
[1] "iteration: 0.95"
[1] "Time points: 19, 76"
[1] "AIC: 243196.2"
[1] "Cox ZPH: 10495.89"
[1] "iteration: 0.975"
[1] "Time points: 19.5, 78"
[1] "AIC: 242775.1"
[1] "Cox ZPH: 10286.2"
[1] "iteration: 1"
[1] "Time points: 20, 80"
[1] "AIC: 242307"
[1] "Cox ZPH: 10071.06"
[1] "iteration: 1.025"
[1] "Time points: 20.5, 82"
[1] "AIC: 241797.4"
[1] "Cox ZPH: 9853.469"
[1] "iteration: 1.05"
[1] "Time points: 21, 84"
[1] "AIC: 241247"
[1] "Cox ZPH: 9636.775"
[1] "iteration: 1.075"
[1] "Time points: 21.5, 86"
[1] "AIC: 240656.4"
[1] "Cox ZPH: 9416.648"
[1] "iteration: 1.1"
[1] "Time points: 22, 88"
[1] "AIC: 240017.6"
[1] "Cox ZPH: 9186.961"
[1] "iteration: 1.125"
[1] "Time points: 22.5, 90"
[1] "AIC: 239332.2"
[1] "Cox ZPH: 8946.098"
[1] "iteration: 1.15"
[1] "Time points: 23, 92"
[1] "AIC: 238596"
[1] "Cox ZPH: 8687.54"
[1] "iteration: 1.175"
[1] "Time points: 23.5, 94"
[1] "AIC: 237808.6"
[1] "Cox ZPH: 8407.242"
[1] "iteration: 1.2"
[1] "Time points: 24, 96"
[1] "AIC: 236965.9"
[1] "Cox ZPH: 8104.911"
[1] "iteration: 1.225"
[1] "Time points: 24.5, 98"
[1] "AIC: 236068.8"
[1] "Cox ZPH: 7780.982"
[1] "iteration: 1.25"
[1] "Time points: 25, 100"
[1] "AIC: 235123.9"
[1] "Cox ZPH: 7435.731"
[1] "iteration: 1.275"
[1] "Time points: 25.5, 102"
[1] "AIC: 234128.5"
[1] "Cox ZPH: 7071.559"
[1] "iteration: 1.3"
[1] "Time points: 26, 104"
[1] "AIC: 233087.8"
[1] "Cox ZPH: 6690.547"
[1] "iteration: 1.325"
[1] "Time points: 26.5, 106"
[1] "AIC: 232023.4"
[1] "Cox ZPH: 6300.418"
[1] "iteration: 1.35"
[1] "Time points: 27, 108"
[1] "AIC: 230934.8"
[1] "Cox ZPH: 5901.547"
[1] "iteration: 1.375"
[1] "Time points: 27.5, 110"
[1] "AIC: 229821"
[1] "Cox ZPH: 5497.145"
[1] "iteration: 1.4"
[1] "Time points: 28, 112"
[1] "AIC: 228696.5"
[1] "Cox ZPH: 5091.223"
[1] "iteration: 1.425"
[1] "Time points: 28.5, 114"
[1] "AIC: 227581"
[1] "Cox ZPH: 4677.408"
[1] "iteration: 1.45"
[1] "Time points: 29, 116"
[1] "AIC: 226445.4"
[1] "Cox ZPH: 4221.169"
[1] "iteration: 1.475"
[1] "Time points: 29.5, 118"
[1] "AIC: 225226.2"
[1] "Cox ZPH: 3672.86"
[1] "iteration: 1.5"
[1] "Time points: 30, 120"
[1] "AIC: 223853.3"
[1] "Cox ZPH: 2998.596"
Code
results_lag_tr_outcome[which(as.numeric(results_lag_tr_outcome$cox_zph)==
                            min(as.numeric(results_lag_tr_outcome$cox_zph))),]
                           spec      aic  cox_zph success
15 iteration: 0.45; tvcs: 9, 36 242741.2 321.6527    TRUE
Code
#                            spec      aic  cox_zph success
# 15 iteration: 0.45; tvcs: 9, 36 242729.5 321.3004    TRUE

message(paste0("The previously modified model: ",
               round(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_alt)$table[nrow(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_alt)$table), 1],1),
               " vs. the minimum model: ",  results_lag_tr_outcome[which(as.numeric(results_lag_tr_outcome$cox_zph)== min(as.numeric(results_lag_tr_outcome$cox_zph))),"cox_zph"]
               ))

The previously modified model: 363.2 vs. the minimum model: 321.6527

Code
#The previously modified model: 361.9 vs. the minimum model: 321.3004


#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
require(splines)

specs_lag_tr_out <- paste0("lag_tr_outcome *", c("time", "log(time+0.001)", "poly(ifelse(is.na(time), .001,time), 3)", "poly(ifelse(is.na(time), .001,time), 4)", "poly(ifelse(is.na(time), .001,time), 5)", "poly(ifelse(is.na(time), .001,time), 6)", "pspline(log(time+0.001), df=3)", "pspline(log(time+0.001), df=4)", "pspline(log(time+0.001), df=5)", "pspline(log(time+0.001), df=6)", "bs(time, 3)", "bs(time, 4)", "bs(time, 5)", "bs(time, 6)", "ns(time, 3)", "ns(time, 4)", "ns(time, 5)", "ns(time, 6)", "rcs(time, 3)", "rcs(time, 4)", "rcs(time, 5)", "rcs(time, 6)"))

results_lag_tr_out <- data.frame(spec = character(), aic = numeric(), cox_zph = numeric(), success = logical())

for (i in seq_along(specs_lag_tr_out)) {
  l <- specs_lag_tr_out[i]

  result <- tryCatch({
    model <- cph(as.formula(paste0("Surv(lag_time,time,event==1)~ 
              cluster(id)+ 
              I(round(",l,",2)) +
              log_lag_dias_treat_imp_sin_na +
              lag_less_90d_tr1_rec+
              lag_comp_bpsc_y3_severe_rec + 
              lag_policonsumo2 + 
              edad_al_ing_1 + 
              ano_nac_corr + 
              susinidum_oh +
              susinidum_coc +
              susinidum_pbc +
              susinidum_mar +
              psycom_dum_with +
              psycom_dum_study + 
              freq_cons_dum_5day +
              cond_oc_dum_2inact +
              cond_oc_dum_3unemp +
              susprindum_oh +
              susprindum_coc +
              susprindum_pbc +
              susprindum_mar+
              strat(tipo_de_plan_2_mod)")), 
          data=data_mine_miss_restr_proc2 %>% 
            dplyr::mutate(log_lag_dias_treat_imp_sin_na_rec = bs(log_lag_dias_treat_imp_sin_na, 3)) %>% 
            data.table::as.data.table() %>% data.frame(), 
           x=TRUE, y=TRUE, surv=TRUE, iter.max = 250*4, tol = 1e-6)

    aic_val <- extractAIC(model)[[2]]
    cox_zph_val <- cox.zph(model)$table[nrow(cox.zph(model)$table), 1]
    success <- TRUE 

  }, error = function(e) {
    aic_val <- NA
    cox_zph_val <- NA
    success <- FALSE
    warning(paste0("Error fitting specification ", i, " (", l, "): ", e))
  })

  results_lag_tr_out <- rbind(results_lag_tr_out, data.frame(spec = l, aic = aic_val, cox_zph = cox_zph_val, success = success))

  print(cat(paste("Iteration:", i, "\n"))) # Add iteration number
  print(cat(paste("Spec: ", l, "\n")))
  print(cat(paste("AIC: ", results_lag_tr_out$aic[i], "\n"))) 
  print(cat(paste("Cox zph test statistic: ", results_lag_tr_out$cox_zph[i], "\n")))
}
Iteration: 1 
NULL
Spec:  lag_tr_outcome *time 
NULL
AIC:  242375.661429718 
NULL
Cox zph test statistic:  4294.36756065618 
NULL
Iteration: 2 
NULL
Spec:  lag_tr_outcome *log(time+0.001) 
NULL
AIC:  244573.478733201 
NULL
Cox zph test statistic:  1250.69604172358 
NULL
Iteration: 3 
NULL
Spec:  lag_tr_outcome *poly(ifelse(is.na(time), .001,time), 3) 
NULL
AIC:  241209.963903167 
NULL
Cox zph test statistic:  12162.2885407214 
NULL
Iteration: 4 
NULL
Spec:  lag_tr_outcome *poly(ifelse(is.na(time), .001,time), 4) 
NULL
AIC:  240784.062529073 
NULL
Cox zph test statistic:  12595.5788006453 
NULL
Iteration: 5 
NULL
Spec:  lag_tr_outcome *poly(ifelse(is.na(time), .001,time), 5) 
NULL
AIC:  240655.695209592 
NULL
Cox zph test statistic:  12721.8186063262 
NULL
Iteration: 6 
NULL
Spec:  lag_tr_outcome *poly(ifelse(is.na(time), .001,time), 6) 
NULL
AIC:  240210.222768023 
NULL
Cox zph test statistic:  12447.9992558718 
NULL
Iteration: 7 
NULL
Spec:  lag_tr_outcome *pspline(log(time+0.001), df=3) 
NULL
AIC:  240210.222768023 
NULL
Cox zph test statistic:  12447.9992558718 
NULL
Iteration: 8 
NULL
Spec:  lag_tr_outcome *pspline(log(time+0.001), df=4) 
NULL
AIC:  240210.222768023 
NULL
Cox zph test statistic:  12447.9992558718 
NULL
Iteration: 9 
NULL
Spec:  lag_tr_outcome *pspline(log(time+0.001), df=5) 
NULL
AIC:  240210.222768023 
NULL
Cox zph test statistic:  12447.9992558718 
NULL
Iteration: 10 
NULL
Spec:  lag_tr_outcome *pspline(log(time+0.001), df=6) 
NULL
AIC:  240210.222768023 
NULL
Cox zph test statistic:  12447.9992558718 
NULL
Iteration: 11 
NULL
Spec:  lag_tr_outcome *bs(time, 3) 
NULL
AIC:  238139.168565678 
NULL
Cox zph test statistic:  20194.0931692 
NULL
Iteration: 12 
NULL
Spec:  lag_tr_outcome *bs(time, 4) 
NULL
AIC:  221388.904711619 
NULL
Cox zph test statistic:  92983.6270487867 
NULL
Iteration: 13 
NULL
Spec:  lag_tr_outcome *bs(time, 5) 
NULL
AIC:  213169.70279823 
NULL
Cox zph test statistic:  77263.2011050867 
NULL
Iteration: 14 
NULL
Spec:  lag_tr_outcome *bs(time, 6) 
NULL
AIC:  211342.28883079 
NULL
Cox zph test statistic:  103010.888687678 
NULL
Iteration: 15 
NULL
Spec:  lag_tr_outcome *ns(time, 3) 
NULL
AIC:  230455.219132658 
NULL
Cox zph test statistic:  50605.4601854322 
NULL
Iteration: 16 
NULL
Spec:  lag_tr_outcome *ns(time, 4) 
NULL
AIC:  219389.439286971 
NULL
Cox zph test statistic:  78509.834057776 
NULL
Iteration: 17 
NULL
Spec:  lag_tr_outcome *ns(time, 5) 
NULL
AIC:  215468.139506862 
NULL
Cox zph test statistic:  83287.572463163 
NULL
Iteration: 18 
NULL
Spec:  lag_tr_outcome *ns(time, 6) 
NULL
AIC:  213583.907149193 
NULL
Cox zph test statistic:  101262.397922225 
NULL
Iteration: 19 
NULL
Spec:  lag_tr_outcome *rcs(time, 3) 
NULL
AIC:  239536.406693165 
NULL
Cox zph test statistic:  17245.5074031239 
NULL
Iteration: 20 
NULL
Spec:  lag_tr_outcome *rcs(time, 4) 
NULL
AIC:  228684.622158062 
NULL
Cox zph test statistic:  65410.1688298938 
NULL
Iteration: 21 
NULL
Spec:  lag_tr_outcome *rcs(time, 5) 
NULL
AIC:  226434.369582259 
NULL
Cox zph test statistic:  23238.4847315333 
NULL
X matrix deemed to be singular; variable time''' 
Iteration: 22 
NULL
Spec:  lag_tr_outcome *rcs(time, 6) 
NULL
AIC:  226434.369582259 
NULL
Cox zph test statistic:  23238.4847315333 
NULL
Code
results_lag_tr_out[which(as.numeric(results_lag_tr_out$cox_zph)==
                                 min(as.numeric(results_lag_tr_out$cox_zph))),]
                             spec      aic  cox_zph success
2 lag_tr_outcome *log(time+0.001) 244573.5 1250.696    TRUE
Code
#                              spec      aic  cox_zph success
# 2 lag_tr_outcome *log(time+0.001) 244561.9 1248.051    TRUE
invisible("It worsen. We do nothing")

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
results_lag_tr_outcome_alt <- data.frame(spec = character(), aic = numeric(), cox_zph = numeric(), success = logical())

for (i in seq(1,5,.1)) {
change_points <- c(5*i, 10*i, 15*i)  # Example change points (months)
coefficients <- c(0.5, 1.5, 0.5, 1)  # Example coefficients
# Add the time-varying coefficient to your model

data_mine_miss_restr_proc2$lag_tr_outcome_rec2 <- with(data_mine_miss_restr_proc2,
                                                 lag_tr_outcome*time_varying_coeff(lag_time, change_points, coefficients))
data_mine_miss_restr_proc2$tr_outcome_rec2 <- with(data_mine_miss_restr_proc2,
                                                 tr_outcome*time_varying_coeff(time, change_points, coefficients))
  
  model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_alt<-
    cph(as.formula(paste0("Surv(lag_time,time,event==1)~ 
              cluster(id)+ 
              lag_tr_outcome_rec2 +
              log_lag_dias_treat_imp_sin_na +
              lag_less_90d_tr1_rec+
              lag_comp_bpsc_y3_severe_rec + 
              lag_policonsumo2 + 
              edad_al_ing_1 + 
              ano_nac_corr + 
              susinidum_oh +
              susinidum_coc +
              susinidum_pbc +
              susinidum_mar +
              psycom_dum_with +
              psycom_dum_study + 
              freq_cons_dum_5day +
              cond_oc_dum_2inact +
              cond_oc_dum_3unemp +
              susprindum_oh +
              susprindum_coc +
              susprindum_pbc +
              susprindum_mar+
              strat(tipo_de_plan_2_mod)")), 
        data=data_mine_miss_restr_proc2 %>% 
          data.table::as.data.table() %>% data.frame(), 
        x=TRUE, y=TRUE, surv=TRUE, iter.max = 250*4, tol = 1e-6)
  
  cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_alt)
  invisible("")
  print(paste0("iteration: ",i))
  aic_iter <- round(extractAIC(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_alt)[[2]],1)

  print( paste0("Time points: ",change_points[1],", ", change_points[2], ", ",change_points[3]))
  print( paste0("AIC: ", aic_iter))
  cox_zph_iter <- format(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_alt)$table[nrow(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_alt)$table), 1])
  print(paste0("Cox ZPH: ", cox_zph_iter))
  
  results_lag_tr_outcome_alt <- rbind.data.frame(results_lag_tr_outcome_alt, 
                                          cbind.data.frame(
                                            spec = paste0("iteration: ",i, "; change points: ", change_points[1],", ", change_points[2], ", ",change_points[3]), 
                                            aic = aic_iter,
                                            cox_zph = cox_zph_iter,
                                            success = T
                                          ))
}
[1] "iteration: 1"
[1] "Time points: 5, 10, 15"
[1] "AIC: 244899.5"
[1] "Cox ZPH: 359.0971"
[1] "iteration: 1.1"
[1] "Time points: 5.5, 11, 16.5"
[1] "AIC: 244900.4"
[1] "Cox ZPH: 361.2743"
[1] "iteration: 1.2"
[1] "Time points: 6, 12, 18"
[1] "AIC: 244903.3"
[1] "Cox ZPH: 358.6406"
[1] "iteration: 1.3"
[1] "Time points: 6.5, 13, 19.5"
[1] "AIC: 244906.3"
[1] "Cox ZPH: 359.1773"
[1] "iteration: 1.4"
[1] "Time points: 7, 14, 21"
[1] "AIC: 244906.5"
[1] "Cox ZPH: 360.6033"
[1] "iteration: 1.5"
[1] "Time points: 7.5, 15, 22.5"
[1] "AIC: 244906.8"
[1] "Cox ZPH: 362.1983"
[1] "iteration: 1.6"
[1] "Time points: 8, 16, 24"
[1] "AIC: 244902.5"
[1] "Cox ZPH: 361.868"
[1] "iteration: 1.7"
[1] "Time points: 8.5, 17, 25.5"
[1] "AIC: 244901.8"
[1] "Cox ZPH: 364.0377"
[1] "iteration: 1.8"
[1] "Time points: 9, 18, 27"
[1] "AIC: 244905.9"
[1] "Cox ZPH: 364.3575"
[1] "iteration: 1.9"
[1] "Time points: 9.5, 19, 28.5"
[1] "AIC: 244905.3"
[1] "Cox ZPH: 365.32"
[1] "iteration: 2"
[1] "Time points: 10, 20, 30"
[1] "AIC: 244901.4"
[1] "Cox ZPH: 368.9659"
[1] "iteration: 2.1"
[1] "Time points: 10.5, 21, 31.5"
[1] "AIC: 244901.9"
[1] "Cox ZPH: 369.6159"
[1] "iteration: 2.2"
[1] "Time points: 11, 22, 33"
[1] "AIC: 244903"
[1] "Cox ZPH: 365.9987"
[1] "iteration: 2.3"
[1] "Time points: 11.5, 23, 34.5"
[1] "AIC: 244895.1"
[1] "Cox ZPH: 361.7019"
[1] "iteration: 2.4"
[1] "Time points: 12, 24, 36"
[1] "AIC: 244896.9"
[1] "Cox ZPH: 356.4047"
[1] "iteration: 2.5"
[1] "Time points: 12.5, 25, 37.5"
[1] "AIC: 244893.9"
[1] "Cox ZPH: 357.1797"
[1] "iteration: 2.6"
[1] "Time points: 13, 26, 39"
[1] "AIC: 244891.6"
[1] "Cox ZPH: 356.8042"
[1] "iteration: 2.7"
[1] "Time points: 13.5, 27, 40.5"
[1] "AIC: 244885.7"
[1] "Cox ZPH: 354.4202"
[1] "iteration: 2.8"
[1] "Time points: 14, 28, 42"
[1] "AIC: 244880.4"
[1] "Cox ZPH: 354.3132"
[1] "iteration: 2.9"
[1] "Time points: 14.5, 29, 43.5"
[1] "AIC: 244879.2"
[1] "Cox ZPH: 352.0868"
[1] "iteration: 3"
[1] "Time points: 15, 30, 45"
[1] "AIC: 244879.9"
[1] "Cox ZPH: 349.6504"
[1] "iteration: 3.1"
[1] "Time points: 15.5, 31, 46.5"
[1] "AIC: 244883.7"
[1] "Cox ZPH: 349.1183"
[1] "iteration: 3.2"
[1] "Time points: 16, 32, 48"
[1] "AIC: 244880.6"
[1] "Cox ZPH: 348.1966"
[1] "iteration: 3.3"
[1] "Time points: 16.5, 33, 49.5"
[1] "AIC: 244877.7"
[1] "Cox ZPH: 348.1207"
[1] "iteration: 3.4"
[1] "Time points: 17, 34, 51"
[1] "AIC: 244881.7"
[1] "Cox ZPH: 348.0226"
[1] "iteration: 3.5"
[1] "Time points: 17.5, 35, 52.5"
[1] "AIC: 244881.8"
[1] "Cox ZPH: 348.0872"
[1] "iteration: 3.6"
[1] "Time points: 18, 36, 54"
[1] "AIC: 244883"
[1] "Cox ZPH: 349.6944"
[1] "iteration: 3.7"
[1] "Time points: 18.5, 37, 55.5"
[1] "AIC: 244883.3"
[1] "Cox ZPH: 350.0009"
[1] "iteration: 3.8"
[1] "Time points: 19, 38, 57"
[1] "AIC: 244880.6"
[1] "Cox ZPH: 350.4467"
[1] "iteration: 3.9"
[1] "Time points: 19.5, 39, 58.5"
[1] "AIC: 244877.4"
[1] "Cox ZPH: 350.8478"
[1] "iteration: 4"
[1] "Time points: 20, 40, 60"
[1] "AIC: 244878.7"
[1] "Cox ZPH: 351.8303"
[1] "iteration: 4.1"
[1] "Time points: 20.5, 41, 61.5"
[1] "AIC: 244880"
[1] "Cox ZPH: 354.9833"
[1] "iteration: 4.2"
[1] "Time points: 21, 42, 63"
[1] "AIC: 244877"
[1] "Cox ZPH: 354.6689"
[1] "iteration: 4.3"
[1] "Time points: 21.5, 43, 64.5"
[1] "AIC: 244873.9"
[1] "Cox ZPH: 355.1347"
[1] "iteration: 4.4"
[1] "Time points: 22, 44, 66"
[1] "AIC: 244871.1"
[1] "Cox ZPH: 356.1269"
[1] "iteration: 4.5"
[1] "Time points: 22.5, 45, 67.5"
[1] "AIC: 244874.5"
[1] "Cox ZPH: 357.1391"
[1] "iteration: 4.6"
[1] "Time points: 23, 46, 69"
[1] "AIC: 244875.8"
[1] "Cox ZPH: 358.4438"
[1] "iteration: 4.7"
[1] "Time points: 23.5, 47, 70.5"
[1] "AIC: 244874.8"
[1] "Cox ZPH: 359.9124"
[1] "iteration: 4.8"
[1] "Time points: 24, 48, 72"
[1] "AIC: 244865.4"
[1] "Cox ZPH: 358.8837"
[1] "iteration: 4.9"
[1] "Time points: 24.5, 49, 73.5"
[1] "AIC: 244868.3"
[1] "Cox ZPH: 360.8408"
[1] "iteration: 5"
[1] "Time points: 25, 50, 75"
[1] "AIC: 244868.9"
[1] "Cox ZPH: 361.8831"
Code
results_lag_tr_outcome_alt[which(as.numeric(results_lag_tr_outcome_alt$cox_zph)==
                            min(as.numeric(results_lag_tr_outcome_alt$cox_zph))),]
                                        spec      aic  cox_zph success
25 iteration: 3.4; change points: 17, 34, 51 244881.7 348.0226    TRUE
Code
#                                         spec      aic  cox_zph success
# 25 iteration: 3.4; change points: 17, 34, 51 244869.2 347.3415    TRUE

message(paste0("The minimum of the first modified model: ",
                results_lag_tr_outcome[which(as.numeric(results_lag_tr_outcome$cox_zph)== min(as.numeric(results_lag_tr_outcome$cox_zph))),"cox_zph"],
               " vs. the minimum model: ",  results_lag_tr_outcome_alt[which(as.numeric(results_lag_tr_outcome_alt$cox_zph)== min(as.numeric(results_lag_tr_outcome_alt$cox_zph))),"cox_zph"]
               ))

The minimum of the first modified model: 321.6527 vs. the minimum model: 348.0226

Code
#The minimum of the first modified model: 321.3004 vs. the minimum model: 347.3415


#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
results_lag_tr_outcome_alt[which(as.numeric(results_lag_tr_outcome$cox_zph)==
                            min(as.numeric(results_lag_tr_outcome$cox_zph))),]
                                        spec      aic  cox_zph success
15 iteration: 2.4; change points: 12, 24, 36 244896.9 356.4047    TRUE
Code
#                            spec      aic  cox_zph success
# 15 iteration: 0.45; tvcs: 9, 36 242729.5 321.3004    TRUE

tvcs<- c(9, 36)

data_mine_miss_restr_proc2$lag_tr_outcome_rec<-
create_time_varying_effect(data_mine_miss_restr_proc2$lag_tr_outcome, 
                   data_mine_miss_restr_proc2$time, 
                   tvcs[1],
                   tvcs[2],
                   max(data_mine_miss_restr_proc2$time))
data_mine_miss_restr_proc2$tr_outcome_rec<-
create_time_varying_effect(data_mine_miss_restr_proc2$tr_outcome, 
                   data_mine_miss_restr_proc2$time, 
                   tvcs[1],
                   tvcs[2],
                   max(data_mine_miss_restr_proc2$time))

model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out<-
  cph(Surv(lag_time,time,event==1)~ 
            cluster(id)+ 
            lag_tr_outcome_rec +
            log_lag_dias_treat_imp_sin_na +
            lag_less_90d_tr1_rec+
            lag_comp_bpsc_y3_severe_rec + 
            lag_policonsumo2 + 
            edad_al_ing_1 + 
            ano_nac_corr + 
            susinidum_oh +
            susinidum_coc +
            susinidum_pbc +
            susinidum_mar +
            psycom_dum_with +
            psycom_dum_study + 
            freq_cons_dum_5day +
            cond_oc_dum_2inact +
            cond_oc_dum_3unemp +
            susprindum_oh +
            susprindum_coc +
            susprindum_pbc +
            susprindum_mar+
            strat(tipo_de_plan_2_mod), 
      data=data_mine_miss_restr_proc2 %>% 
        data.table::as.data.table() %>% data.frame(), 
      x=TRUE, y=TRUE, surv=TRUE, iter.max = 250*4, tol = 1e-6)

round(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out)$table,4)
                                 chisq df      p
lag_tr_outcome_rec             66.5194  1 0.0000
log_lag_dias_treat_imp_sin_na  10.3514  1 0.0013
lag_less_90d_tr1_rec           84.0336  1 0.0000
lag_comp_bpsc_y3_severe_rec    17.2563  1 0.0000
lag_policonsumo2                3.2167  1 0.0729
edad_al_ing_1                   0.1446  1 0.7038
ano_nac_corr                    5.1810  1 0.0228
susinidum_oh                   20.1319  1 0.0000
susinidum_coc                   9.7922  1 0.0018
susinidum_pbc                   2.0721  1 0.1500
susinidum_mar                   6.7050  1 0.0096
psycom_dum_with                11.7329  1 0.0006
psycom_dum_study                1.6100  1 0.2045
freq_cons_dum_5day              0.0801  1 0.7772
cond_oc_dum_2inact              4.4364  1 0.0352
cond_oc_dum_3unemp              1.7433  1 0.1867
susprindum_oh                   4.4295  1 0.0353
susprindum_coc                  1.6332  1 0.2013
susprindum_pbc                  0.0553  1 0.8141
susprindum_mar                  5.2895  1 0.0215
GLOBAL                        321.6527 20 0.0000
Code
#                                  chisq df      p
# lag_tr_outcome_rec             66.6269  1 0.0000 ####
# log_lag_dias_treat_imp_sin_na  10.1949  1 0.0014 #
# lag_less_90d_tr1_rec           84.0361  1 0.0000 ####
# lag_comp_bpsc_y3_severe_rec    17.2093  1 0.0000 ####
# lag_policonsumo2                3.1849  1 0.0743
# edad_al_ing_1                   0.1368  1 0.7115
# ano_nac_corr                    5.2180  1 0.0224 #
# susinidum_oh                   19.9277  1 0.0000 ####
# susinidum_coc                   9.7729  1 0.0018 ##
# susinidum_pbc                   1.9935  1 0.1580
# susinidum_mar                   6.6712  1 0.0098 #
# psycom_dum_with                11.7874  1 0.0006 ##
# psycom_dum_study                1.6215  1 0.2029
# freq_cons_dum_5day              0.0521  1 0.8194
# cond_oc_dum_2inact              4.4607  1 0.0347
# cond_oc_dum_3unemp              1.6238  1 0.2026
# susprindum_oh                   4.3416  1 0.0372
# susprindum_coc                  1.6713  1 0.1961
# susprindum_pbc                  0.0723  1 0.7879
# susprindum_mar                  5.3392  1 0.0209
# GLOBAL                        321.3004 20 0.0000

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
invisible("Analyse the violation of proportional hazards")
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
try(dev.off())
null device 
          1 
Code
res240422_5_restr <- resid(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out, "scaledsch")
time240422_5_restr <- as.numeric(dimnames(res240422_5_restr)[[1]])

z_240422_5_1_restr <- loess(res240422_5_restr[,"susinidum_oh"] ~ time240422_5_restr, span=0.50)   # residuals 

par(mfrow= c(3,1))
plot(time240422_5_restr, fitted(z_240422_5_1_restr))
lines(supsmu(time240422_5_restr, res240422_5_restr[,"susinidum_oh"]),lty=2)
title("Sch. res., susinidum_oh")

plot(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out)[which(attr(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out$coefficients, "names") == "susinidum_oh")], lwd=2, main= "Residuals of susinidum_oh")
abline(0,0, col="red", lty=3, lwd=2)
abline(h=model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out$coefficients["susinidum_oh"], col=3, lwd=2, lty= 3)
legend("bottomleft", legend= c("Reference line for the null effect", "Average hazard over time", "Time-varying hazard"), lty=c(3,2,1), col= c("red",3,1), lwd=2, bty = "n", bg = "transparent")

km_susinidum_oh <- survfit(Surv(lag_time,time,event)~ susinidum_oh+ 
    cluster(id), data = data_mine_miss_restr_proc2)
plot(km_susinidum_oh, fun = "cloglog", xlab = "Time (in days) using log",col=c("black", "red"), ylab = "log-log survival", main = "log-log curves by susinidum_oh")
legend("bottomright", legend=c("=0", "=1"), col=c("black", "red"), lty=1, bty = "n", bg = "transparent")

recorded_plot <- recordPlot() 

folder_path <- ifelse(dir.exists("E:/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/"),
                      "E:/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/",
                      "C:/Users/CISS Fondecyt/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/")
png(paste0(folder_path,"zph/proportionallity_susinidum_oh_restr.png"), height=13, width=10, res=500, units="in") 
recorded_plot
dev.off()
pdf 
  2 
Code
invisible("Now with Initial substance: Alcohol")

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
results_susinidum_oh <- data.frame(spec = character(), aic = numeric(), cox_zph = numeric(), success = logical())

for (i in seq(.1,1.5,.025)) {
  tvcs <- c(20,80)*i
  
  data_mine_miss_restr_proc2$susinidum_oh_rec<-
    create_time_varying_effect(data_mine_miss_restr_proc2$susinidum_oh, 
                               data_mine_miss_restr_proc2$time, 
                               tvcs[1],
                               tvcs[2],
                               max(data_mine_miss_restr_proc2$time))
  
  model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_oh<-
    cph(as.formula(paste0("Surv(lag_time,time,event==1)~ 
            cluster(id)+ 
            lag_tr_outcome_rec +
            log_lag_dias_treat_imp_sin_na +
            lag_less_90d_tr1_rec+
            lag_comp_bpsc_y3_severe_rec + 
            lag_policonsumo2 + 
            edad_al_ing_1 + 
            ano_nac_corr + 
            susinidum_oh_rec +
            susinidum_coc +
            susinidum_pbc +
            susinidum_mar +
            psycom_dum_with +
            psycom_dum_study + 
            freq_cons_dum_5day +
            cond_oc_dum_2inact +
            cond_oc_dum_3unemp +
            susprindum_oh +
            susprindum_coc +
            susprindum_pbc +
            susprindum_mar+
            strat(tipo_de_plan_2_mod)")), 
        data=data_mine_miss_restr_proc2 %>% 
          data.table::as.data.table() %>% data.frame(), 
        x=TRUE, y=TRUE, surv=TRUE, iter.max = 250*4, tol = 1e-6)
  
  cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_oh)
  invisible("")
  print(paste0("iteration: ",i))
  aic_iter <- round(extractAIC(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_oh)[[2]],1)
  print( paste0("Time points: ",tvcs[1],", ", tvcs[2]))
  print( paste0("AIC: ", aic_iter))
  cox_zph_iter <- format(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_oh)$table[nrow(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_oh)$table), 1])
  print(paste0("Cox ZPH: ", cox_zph_iter))
  
  results_susinidum_oh <- rbind.data.frame(results_susinidum_oh, 
                                             cbind.data.frame(
                                               spec = paste0("iteration: ",i, "; tvcs: ", tvcs[1],", ", tvcs[2]), 
                                               aic = aic_iter,
                                               cox_zph = cox_zph_iter,
                                               success = T
                                             ))
}
[1] "iteration: 0.1"
[1] "Time points: 2, 8"
[1] "AIC: 239429.8"
[1] "Cox ZPH: 5741.105"
[1] "iteration: 0.125"
[1] "Time points: 2.5, 10"
[1] "AIC: 239392.7"
[1] "Cox ZPH: 5911.203"
[1] "iteration: 0.15"
[1] "Time points: 3, 12"
[1] "AIC: 239385.1"
[1] "Cox ZPH: 5995.153"
[1] "iteration: 0.175"
[1] "Time points: 3.5, 14"
[1] "AIC: 239405.8"
[1] "Cox ZPH: 6004.474"
[1] "iteration: 0.2"
[1] "Time points: 4, 16"
[1] "AIC: 239458"
[1] "Cox ZPH: 5931.333"
[1] "iteration: 0.225"
[1] "Time points: 4.5, 18"
[1] "AIC: 239533.6"
[1] "Cox ZPH: 5798.068"
[1] "iteration: 0.25"
[1] "Time points: 5, 20"
[1] "AIC: 239629.6"
[1] "Cox ZPH: 5605.943"
[1] "iteration: 0.275"
[1] "Time points: 5.5, 22"
[1] "AIC: 239746.7"
[1] "Cox ZPH: 5345.649"
[1] "iteration: 0.3"
[1] "Time points: 6, 24"
[1] "AIC: 239888"
[1] "Cox ZPH: 4997.661"
[1] "iteration: 0.325"
[1] "Time points: 6.5, 26"
[1] "AIC: 240042.6"
[1] "Cox ZPH: 4586.522"
[1] "iteration: 0.35"
[1] "Time points: 7, 28"
[1] "AIC: 240211.5"
[1] "Cox ZPH: 4104.321"
[1] "iteration: 0.375"
[1] "Time points: 7.5, 30"
[1] "AIC: 240387.4"
[1] "Cox ZPH: 3584.708"
[1] "iteration: 0.4"
[1] "Time points: 8, 32"
[1] "AIC: 240565.7"
[1] "Cox ZPH: 3057.196"
[1] "iteration: 0.425"
[1] "Time points: 8.5, 34"
[1] "AIC: 240747.1"
[1] "Cox ZPH: 2541.069"
[1] "iteration: 0.45"
[1] "Time points: 9, 36"
[1] "AIC: 240930.5"
[1] "Cox ZPH: 2078.364"
[1] "iteration: 0.475"
[1] "Time points: 9.5, 38"
[1] "AIC: 241113.3"
[1] "Cox ZPH: 1728.213"
[1] "iteration: 0.5"
[1] "Time points: 10, 40"
[1] "AIC: 241288.4"
[1] "Cox ZPH: 1549.536"
[1] "iteration: 0.525"
[1] "Time points: 10.5, 42"
[1] "AIC: 241459.6"
[1] "Cox ZPH: 1566.979"
[1] "iteration: 0.55"
[1] "Time points: 11, 44"
[1] "AIC: 241626.8"
[1] "Cox ZPH: 1794.567"
[1] "iteration: 0.575"
[1] "Time points: 11.5, 46"
[1] "AIC: 241787.2"
[1] "Cox ZPH: 2219.701"
[1] "iteration: 0.6"
[1] "Time points: 12, 48"
[1] "AIC: 241939.2"
[1] "Cox ZPH: 2804.371"
[1] "iteration: 0.625"
[1] "Time points: 12.5, 50"
[1] "AIC: 242083.7"
[1] "Cox ZPH: 3505.791"
[1] "iteration: 0.65"
[1] "Time points: 13, 52"
[1] "AIC: 242219.9"
[1] "Cox ZPH: 4264.682"
[1] "iteration: 0.675"
[1] "Time points: 13.5, 54"
[1] "AIC: 242345.1"
[1] "Cox ZPH: 5015.719"
[1] "iteration: 0.7"
[1] "Time points: 14, 56"
[1] "AIC: 242456.5"
[1] "Cox ZPH: 5696.982"
[1] "iteration: 0.725"
[1] "Time points: 14.5, 58"
[1] "AIC: 242551.4"
[1] "Cox ZPH: 6259.37"
[1] "iteration: 0.75"
[1] "Time points: 15, 60"
[1] "AIC: 242628.8"
[1] "Cox ZPH: 6683.401"
[1] "iteration: 0.775"
[1] "Time points: 15.5, 62"
[1] "AIC: 242687.4"
[1] "Cox ZPH: 6956.512"
[1] "iteration: 0.8"
[1] "Time points: 16, 64"
[1] "AIC: 242725.2"
[1] "Cox ZPH: 7085.722"
[1] "iteration: 0.825"
[1] "Time points: 16.5, 66"
[1] "AIC: 242740.8"
[1] "Cox ZPH: 7091.269"
[1] "iteration: 0.85"
[1] "Time points: 17, 68"
[1] "AIC: 242733.4"
[1] "Cox ZPH: 6995.256"
[1] "iteration: 0.875"
[1] "Time points: 17.5, 70"
[1] "AIC: 242701.7"
[1] "Cox ZPH: 6819.604"
[1] "iteration: 0.9"
[1] "Time points: 18, 72"
[1] "AIC: 242645.4"
[1] "Cox ZPH: 6586.407"
[1] "iteration: 0.925"
[1] "Time points: 18.5, 74"
[1] "AIC: 242565.4"
[1] "Cox ZPH: 6318.07"
[1] "iteration: 0.95"
[1] "Time points: 19, 76"
[1] "AIC: 242461.6"
[1] "Cox ZPH: 6026.228"
[1] "iteration: 0.975"
[1] "Time points: 19.5, 78"
[1] "AIC: 242333.9"
[1] "Cox ZPH: 5721.657"
[1] "iteration: 1"
[1] "Time points: 20, 80"
[1] "AIC: 242181.2"
[1] "Cox ZPH: 5408.085"
[1] "iteration: 1.025"
[1] "Time points: 20.5, 82"
[1] "AIC: 242004.9"
[1] "Cox ZPH: 5098.267"
[1] "iteration: 1.05"
[1] "Time points: 21, 84"
[1] "AIC: 241807"
[1] "Cox ZPH: 4801.207"
[1] "iteration: 1.075"
[1] "Time points: 21.5, 86"
[1] "AIC: 241592"
[1] "Cox ZPH: 4522.922"
[1] "iteration: 1.1"
[1] "Time points: 22, 88"
[1] "AIC: 241359.7"
[1] "Cox ZPH: 4260.851"
[1] "iteration: 1.125"
[1] "Time points: 22.5, 90"
[1] "AIC: 241110.3"
[1] "Cox ZPH: 4013.58"
[1] "iteration: 1.15"
[1] "Time points: 23, 92"
[1] "AIC: 240849.8"
[1] "Cox ZPH: 3785.966"
[1] "iteration: 1.175"
[1] "Time points: 23.5, 94"
[1] "AIC: 240579.4"
[1] "Cox ZPH: 3575.447"
[1] "iteration: 1.2"
[1] "Time points: 24, 96"
[1] "AIC: 240299.9"
[1] "Cox ZPH: 3386.906"
[1] "iteration: 1.225"
[1] "Time points: 24.5, 98"
[1] "AIC: 240018.9"
[1] "Cox ZPH: 3223.323"
[1] "iteration: 1.25"
[1] "Time points: 25, 100"
[1] "AIC: 239740.7"
[1] "Cox ZPH: 3085.226"
[1] "iteration: 1.275"
[1] "Time points: 25.5, 102"
[1] "AIC: 239466.2"
[1] "Cox ZPH: 2973.787"
[1] "iteration: 1.3"
[1] "Time points: 26, 104"
[1] "AIC: 239195"
[1] "Cox ZPH: 2887.063"
[1] "iteration: 1.325"
[1] "Time points: 26.5, 106"
[1] "AIC: 238931.4"
[1] "Cox ZPH: 2829.198"
[1] "iteration: 1.35"
[1] "Time points: 27, 108"
[1] "AIC: 238672.7"
[1] "Cox ZPH: 2804.632"
[1] "iteration: 1.375"
[1] "Time points: 27.5, 110"
[1] "AIC: 238420.6"
[1] "Cox ZPH: 2817.714"
[1] "iteration: 1.4"
[1] "Time points: 28, 112"
[1] "AIC: 238188.2"
[1] "Cox ZPH: 2873.441"
[1] "iteration: 1.425"
[1] "Time points: 28.5, 114"
[1] "AIC: 237975.4"
[1] "Cox ZPH: 2981.972"
[1] "iteration: 1.45"
[1] "Time points: 29, 116"
[1] "AIC: 237779.2"
[1] "Cox ZPH: 3159.954"
[1] "iteration: 1.475"
[1] "Time points: 29.5, 118"
[1] "AIC: 237606.6"
[1] "Cox ZPH: 3430.731"
[1] "iteration: 1.5"
[1] "Time points: 30, 120"
[1] "AIC: 237464"
[1] "Cox ZPH: 3868.406"
Code
results_susinidum_oh[which(as.numeric(results_susinidum_oh$cox_zph)==
                               min(as.numeric(results_susinidum_oh$cox_zph))),]
                           spec      aic  cox_zph success
17 iteration: 0.5; tvcs: 10, 40 241288.4 1549.536    TRUE
Code
#                            spec      aic  cox_zph success
# 15 iteration: 0.45; tvcs: 9, 36 242729.5 321.3004    TRUE

message(paste0("The previously modified model: ",
               round(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out)$table[nrow(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out)$table), 1],1),
               " vs. the minimum model: ",  results_susinidum_oh[which(as.numeric(results_susinidum_oh$cox_zph)== min(as.numeric(results_susinidum_oh$cox_zph))),"cox_zph"]
))

The previously modified model: 321.7 vs. the minimum model: 1549.536

Code
#The previously modified model: 321.3 vs. the minimum model: 1547.708
invisible("It worsen")

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
require(splines)

specs_susinidum_oh <- paste0("susinidum_oh *", c("time", "log(time+0.001)", "poly(ifelse(is.na(time), .001,time), 3)", "poly(ifelse(is.na(time), .001,time), 4)", "poly(ifelse(is.na(time), .001,time), 5)", "poly(ifelse(is.na(time), .001,time), 6)", "pspline(log(time+0.001), df=3)", "pspline(log(time+0.001), df=4)", "pspline(log(time+0.001), df=5)", "pspline(log(time+0.001), df=6)", "bs(time, 3)", "bs(time, 4)", "bs(time, 5)", "bs(time, 6)", "ns(time, 3)", "ns(time, 4)", "ns(time, 5)", "ns(time, 6)", "rcs(time, 3)", "rcs(time, 4)", "rcs(time, 5)", "rcs(time, 6)"))

results_susini_oh <- data.frame(spec = character(), aic = numeric(), cox_zph = numeric(), success = logical())

for (i in seq_along(specs_susinidum_oh)) {
  l <- specs_susinidum_oh[i]
  
    
  result <- tryCatch({
    model <- cph(as.formula(paste0("Surv(lag_time,time,event==1)~ 
            cluster(id)+ 
            lag_tr_outcome_rec +
            log_lag_dias_treat_imp_sin_na +
            lag_less_90d_tr1_rec+
            lag_comp_bpsc_y3_severe_rec + 
            lag_policonsumo2 + 
            edad_al_ing_1 + 
            ano_nac_corr + 
            I(round(",l,",2)) +
            susinidum_coc +
            susinidum_pbc +
            susinidum_mar +
            psycom_dum_with +
            psycom_dum_study + 
            freq_cons_dum_5day +
            cond_oc_dum_2inact +
            cond_oc_dum_3unemp +
            susprindum_oh +
            susprindum_coc +
            susprindum_pbc +
            susprindum_mar+
            strat(tipo_de_plan_2_mod)")), 
                 data=data_mine_miss_restr_proc2 %>% 
                   dplyr::mutate(log_lag_dias_treat_imp_sin_na_rec = bs(log_lag_dias_treat_imp_sin_na, 3)) %>% 
                   data.table::as.data.table() %>% data.frame(), 
                 x=TRUE, y=TRUE, surv=TRUE, iter.max = 250*4, tol = 1e-6)
    
    aic_val <- extractAIC(model)[[2]]
    cox_zph_val <- cox.zph(model)$table[nrow(cox.zph(model)$table), 1]
    success <- TRUE 
    
  }, error = function(e) {
    aic_val <- NA
    cox_zph_val <- NA
    success <- FALSE
    warning(paste0("Error fitting specification ", i, " (", l, "): ", e))
  })
  
  results_susini_oh <- rbind(results_susini_oh, data.frame(spec = l, aic = aic_val, cox_zph = cox_zph_val, success = success))
  
  print(cat(paste("Iteration:", i, "\n"))) # Add iteration number
  print(cat(paste("Spec: ", l, "\n")))
  print(cat(paste("AIC: ", results_susini_oh$aic[i], "\n"))) 
  print(cat(paste("Cox zph test statistic: ", results_susini_oh$cox_zph[i], "\n")))
}
Iteration: 1 
NULL
Spec:  susinidum_oh *time 
NULL
AIC:  239724.019577566 
NULL
Cox zph test statistic:  4672.22758383257 
NULL
Iteration: 2 
NULL
Spec:  susinidum_oh *log(time+0.001) 
NULL
AIC:  241585.150871146 
NULL
Cox zph test statistic:  1204.10177494143 
NULL
Iteration: 3 
NULL
Spec:  susinidum_oh *poly(ifelse(is.na(time), .001,time), 3) 
NULL
AIC:  240219.630415016 
NULL
Cox zph test statistic:  7926.39955359493 
NULL
Iteration: 4 
NULL
Spec:  susinidum_oh *poly(ifelse(is.na(time), .001,time), 4) 
NULL
AIC:  240063.039259188 
NULL
Cox zph test statistic:  8212.21889149851 
NULL
Iteration: 5 
NULL
Spec:  susinidum_oh *poly(ifelse(is.na(time), .001,time), 5) 
NULL
AIC:  240016.095427946 
NULL
Cox zph test statistic:  8304.47329075385 
NULL
Iteration: 6 
NULL
Spec:  susinidum_oh *poly(ifelse(is.na(time), .001,time), 6) 
NULL
AIC:  239508.178249717 
NULL
Cox zph test statistic:  8101.01468172362 
NULL
Iteration: 7 
NULL
Spec:  susinidum_oh *pspline(log(time+0.001), df=3) 
NULL
AIC:  239508.178249717 
NULL
Cox zph test statistic:  8101.01468172362 
NULL
Iteration: 8 
NULL
Spec:  susinidum_oh *pspline(log(time+0.001), df=4) 
NULL
AIC:  239508.178249717 
NULL
Cox zph test statistic:  8101.01468172362 
NULL
Iteration: 9 
NULL
Spec:  susinidum_oh *pspline(log(time+0.001), df=5) 
NULL
AIC:  239508.178249717 
NULL
Cox zph test statistic:  8101.01468172362 
NULL
Iteration: 10 
NULL
Spec:  susinidum_oh *pspline(log(time+0.001), df=6) 
NULL
AIC:  239508.178249717 
NULL
Cox zph test statistic:  8101.01468172362 
NULL
Iteration: 11 
NULL
Spec:  susinidum_oh *bs(time, 3) 
NULL
AIC:  239518.026724213 
NULL
Cox zph test statistic:  10331.3482921498 
NULL
Iteration: 12 
NULL
Spec:  susinidum_oh *bs(time, 4) 
NULL
AIC:  236772.640457164 
NULL
Cox zph test statistic:  18133.928243881 
NULL
Iteration: 13 
NULL
Spec:  susinidum_oh *bs(time, 5) 
NULL
AIC:  235970.965928735 
NULL
Cox zph test statistic:  20900.8309746657 
NULL
Iteration: 14 
NULL
Spec:  susinidum_oh *bs(time, 6) 
NULL
AIC:  235677.262806318 
NULL
Cox zph test statistic:  21727.8304904265 
NULL
Iteration: 15 
NULL
Spec:  susinidum_oh *ns(time, 3) 
NULL
AIC:  238253.396508616 
NULL
Cox zph test statistic:  14728.7767838593 
NULL
Iteration: 16 
NULL
Spec:  susinidum_oh *ns(time, 4) 
NULL
AIC:  236653.226858842 
NULL
Cox zph test statistic:  19308.1808541226 
NULL
Iteration: 17 
NULL
Spec:  susinidum_oh *ns(time, 5) 
NULL
AIC:  236251.069626771 
NULL
Cox zph test statistic:  20993.031671471 
NULL
Iteration: 18 
NULL
Spec:  susinidum_oh *ns(time, 6) 
NULL
AIC:  235896.939555169 
NULL
Cox zph test statistic:  21101.9090902985 
NULL
Iteration: 19 
NULL
Spec:  susinidum_oh *rcs(time, 3) 
NULL
AIC:  239671.217452014 
NULL
Cox zph test statistic:  9313.41322424369 
NULL
X matrix deemed to be singular; variable time'' 
Iteration: 20 
NULL
Spec:  susinidum_oh *rcs(time, 4) 
NULL
AIC:  239671.217452014 
NULL
Cox zph test statistic:  9313.41322424369 
NULL
X matrix deemed to be singular; variable time'' time''' 
Iteration: 21 
NULL
Spec:  susinidum_oh *rcs(time, 5) 
NULL
AIC:  239671.217452014 
NULL
Cox zph test statistic:  9313.41322424369 
NULL
X matrix deemed to be singular; variable time'' time''' 
Iteration: 22 
NULL
Spec:  susinidum_oh *rcs(time, 6) 
NULL
AIC:  239671.217452014 
NULL
Cox zph test statistic:  9313.41322424369 
NULL
Code
results_susini_oh[which(as.numeric(results_susini_oh$cox_zph)==
                           min(as.numeric(results_susini_oh$cox_zph))),]
                           spec      aic  cox_zph success
2 susinidum_oh *log(time+0.001) 241585.2 1204.102    TRUE
Code
#                            spec    aic cox_zph success
# 2 susinidum_oh *log(time+0.001) 241573 1203.69    TRUE
invisible("It worsen. We do nothing")

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
results_susinidum_oh_alt <- data.frame(spec = character(), aic = numeric(), cox_zph = numeric(), success = logical())

for (i in seq(1,5,.1)) {
  change_points <- c(5*i, 10*i, 15*i)  # Example change points (months)
  coefficients <- c(0.5, 1.5, 0.5, 1)  # Example coefficients
  # Add the time-varying coefficient to your model
  
  data_mine_miss_restr_proc2$susinidum_oh_rec2 <- with(data_mine_miss_restr_proc2,
                                                       susinidum_oh*time_varying_coeff(time, change_points, coefficients))

  model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_oh_alt<-
    cph(as.formula(paste0("Surv(lag_time,time,event==1)~ 
            cluster(id)+ 
            lag_tr_outcome_rec +
            log_lag_dias_treat_imp_sin_na +
            lag_less_90d_tr1_rec+
            lag_comp_bpsc_y3_severe_rec + 
            lag_policonsumo2 + 
            edad_al_ing_1 + 
            ano_nac_corr + 
            susinidum_oh_rec2 +
            susinidum_coc +
            susinidum_pbc +
            susinidum_mar +
            psycom_dum_with +
            psycom_dum_study + 
            freq_cons_dum_5day +
            cond_oc_dum_2inact +
            cond_oc_dum_3unemp +
            susprindum_oh +
            susprindum_coc +
            susprindum_pbc +
            susprindum_mar+
            strat(tipo_de_plan_2_mod)")), 
        data=data_mine_miss_restr_proc2 %>% 
          data.table::as.data.table() %>% data.frame(), 
        x=TRUE, y=TRUE, surv=TRUE, iter.max = 250*4, tol = 1e-6)
  
  cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_oh_alt)
  invisible("")
  print(paste0("iteration: ",i))
  aic_iter <- round(extractAIC(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_oh_alt)[[2]],1)
  
  print( paste0("Time points: ",change_points[1],", ", change_points[2], ", ",change_points[3]))
  print( paste0("AIC: ", aic_iter))
  cox_zph_iter <- format(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_oh_alt)$table[nrow(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_oh_alt)$table), 1])
  print(paste0("Cox ZPH: ", cox_zph_iter))
  
  results_susinidum_oh_alt <- rbind.data.frame(results_susinidum_oh_alt, 
                                                 cbind.data.frame(
                                                   spec = paste0("iteration: ",i, "; change points: ", change_points[1],", ", change_points[2], ", ",change_points[3]), 
                                                   aic = aic_iter,
                                                   cox_zph = cox_zph_iter,
                                                   success = T
                                                 ))
}
[1] "iteration: 1"
[1] "Time points: 5, 10, 15"
[1] "AIC: 242702.9"
[1] "Cox ZPH: 345.489"
[1] "iteration: 1.1"
[1] "Time points: 5.5, 11, 16.5"
[1] "AIC: 242711.2"
[1] "Cox ZPH: 329.665"
[1] "iteration: 1.2"
[1] "Time points: 6, 12, 18"
[1] "AIC: 242707.8"
[1] "Cox ZPH: 329.167"
[1] "iteration: 1.3"
[1] "Time points: 6.5, 13, 19.5"
[1] "AIC: 242722"
[1] "Cox ZPH: 337.8074"
[1] "iteration: 1.4"
[1] "Time points: 7, 14, 21"
[1] "AIC: 242730.6"
[1] "Cox ZPH: 360.014"
[1] "iteration: 1.5"
[1] "Time points: 7.5, 15, 22.5"
[1] "AIC: 242736.7"
[1] "Cox ZPH: 396.3886"
[1] "iteration: 1.6"
[1] "Time points: 8, 16, 24"
[1] "AIC: 242739.4"
[1] "Cox ZPH: 415.7995"
[1] "iteration: 1.7"
[1] "Time points: 8.5, 17, 25.5"
[1] "AIC: 242741.2"
[1] "Cox ZPH: 449.8164"
[1] "iteration: 1.8"
[1] "Time points: 9, 18, 27"
[1] "AIC: 242740.8"
[1] "Cox ZPH: 465.6994"
[1] "iteration: 1.9"
[1] "Time points: 9.5, 19, 28.5"
[1] "AIC: 242739"
[1] "Cox ZPH: 484.3736"
[1] "iteration: 2"
[1] "Time points: 10, 20, 30"
[1] "AIC: 242735.2"
[1] "Cox ZPH: 495.5218"
[1] "iteration: 2.1"
[1] "Time points: 10.5, 21, 31.5"
[1] "AIC: 242732.2"
[1] "Cox ZPH: 468.4899"
[1] "iteration: 2.2"
[1] "Time points: 11, 22, 33"
[1] "AIC: 242722.2"
[1] "Cox ZPH: 451.4285"
[1] "iteration: 2.3"
[1] "Time points: 11.5, 23, 34.5"
[1] "AIC: 242718.2"
[1] "Cox ZPH: 423.5481"
[1] "iteration: 2.4"
[1] "Time points: 12, 24, 36"
[1] "AIC: 242716.3"
[1] "Cox ZPH: 395.6226"
[1] "iteration: 2.5"
[1] "Time points: 12.5, 25, 37.5"
[1] "AIC: 242716.8"
[1] "Cox ZPH: 364.4011"
[1] "iteration: 2.6"
[1] "Time points: 13, 26, 39"
[1] "AIC: 242717.5"
[1] "Cox ZPH: 340.7294"
[1] "iteration: 2.7"
[1] "Time points: 13.5, 27, 40.5"
[1] "AIC: 242720.7"
[1] "Cox ZPH: 331.1452"
[1] "iteration: 2.8"
[1] "Time points: 14, 28, 42"
[1] "AIC: 242724.9"
[1] "Cox ZPH: 334.6488"
[1] "iteration: 2.9"
[1] "Time points: 14.5, 29, 43.5"
[1] "AIC: 242729.4"
[1] "Cox ZPH: 353.4168"
[1] "iteration: 3"
[1] "Time points: 15, 30, 45"
[1] "AIC: 242733.9"
[1] "Cox ZPH: 395.1191"
[1] "iteration: 3.1"
[1] "Time points: 15.5, 31, 46.5"
[1] "AIC: 242737"
[1] "Cox ZPH: 455.8316"
[1] "iteration: 3.2"
[1] "Time points: 16, 32, 48"
[1] "AIC: 242740.4"
[1] "Cox ZPH: 524.5127"
[1] "iteration: 3.3"
[1] "Time points: 16.5, 33, 49.5"
[1] "AIC: 242741.2"
[1] "Cox ZPH: 601.5584"
[1] "iteration: 3.4"
[1] "Time points: 17, 34, 51"
[1] "AIC: 242740.6"
[1] "Cox ZPH: 686.4199"
[1] "iteration: 3.5"
[1] "Time points: 17.5, 35, 52.5"
[1] "AIC: 242738.5"
[1] "Cox ZPH: 771.4445"
[1] "iteration: 3.6"
[1] "Time points: 18, 36, 54"
[1] "AIC: 242733.6"
[1] "Cox ZPH: 868.9633"
[1] "iteration: 3.7"
[1] "Time points: 18.5, 37, 55.5"
[1] "AIC: 242730.4"
[1] "Cox ZPH: 951.8658"
[1] "iteration: 3.8"
[1] "Time points: 19, 38, 57"
[1] "AIC: 242721.3"
[1] "Cox ZPH: 1085.059"
[1] "iteration: 3.9"
[1] "Time points: 19.5, 39, 58.5"
[1] "AIC: 242709.4"
[1] "Cox ZPH: 1204.189"
[1] "iteration: 4"
[1] "Time points: 20, 40, 60"
[1] "AIC: 242693.7"
[1] "Cox ZPH: 1337.112"
[1] "iteration: 4.1"
[1] "Time points: 20.5, 41, 61.5"
[1] "AIC: 242682"
[1] "Cox ZPH: 1438.932"
[1] "iteration: 4.2"
[1] "Time points: 21, 42, 63"
[1] "AIC: 242664.9"
[1] "Cox ZPH: 1562.72"
[1] "iteration: 4.3"
[1] "Time points: 21.5, 43, 64.5"
[1] "AIC: 242651.1"
[1] "Cox ZPH: 1674.387"
[1] "iteration: 4.4"
[1] "Time points: 22, 44, 66"
[1] "AIC: 242620.8"
[1] "Cox ZPH: 1769.552"
[1] "iteration: 4.5"
[1] "Time points: 22.5, 45, 67.5"
[1] "AIC: 242607.7"
[1] "Cox ZPH: 1846.276"
[1] "iteration: 4.6"
[1] "Time points: 23, 46, 69"
[1] "AIC: 242585.1"
[1] "Cox ZPH: 1908.988"
[1] "iteration: 4.7"
[1] "Time points: 23.5, 47, 70.5"
[1] "AIC: 242569.1"
[1] "Cox ZPH: 1995.936"
[1] "iteration: 4.8"
[1] "Time points: 24, 48, 72"
[1] "AIC: 242551.2"
[1] "Cox ZPH: 2104.284"
[1] "iteration: 4.9"
[1] "Time points: 24.5, 49, 73.5"
[1] "AIC: 242534.8"
[1] "Cox ZPH: 2177.389"
[1] "iteration: 5"
[1] "Time points: 25, 50, 75"
[1] "AIC: 242517.7"
[1] "Cox ZPH: 2242.511"
Code
results_susinidum_oh_alt[which(as.numeric(results_susinidum_oh_alt$cox_zph)==
                                   min(as.numeric(results_susinidum_oh_alt$cox_zph))),]
                                      spec      aic cox_zph success
3 iteration: 1.2; change points: 6, 12, 18 242707.8 329.167    TRUE
Code
#                                       spec    aic  cox_zph success
# 3 iteration: 1.2; change points: 6, 12, 18 242696 328.8011    TRUE

message(paste0("The minimum of the first modified model: ",
               format(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out)$table[nrow(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out)$table), 1]),
               " vs. the minimum model: ",  results_susinidum_oh_alt[which(as.numeric(results_susinidum_oh_alt$cox_zph)== min(as.numeric(results_susinidum_oh_alt$cox_zph))),"cox_zph"]
))

The minimum of the first modified model: 321.6527 vs. the minimum model: 329.167

Code
#The minimum of the first modified model: 321.3004 vs. the minimum model: 328.8011

invisible("It worsens. We do not integrate this model")

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
invisible("Analyse the violation of proportional hazards")
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
try(dev.off())
null device 
          1 
Code
z_240422_5_2_restr <- loess(res240422_5_restr[,"susinidum_coc"] ~ time240422_5_restr, span=0.50)   # residuals 

par(mfrow= c(3,1))
plot(time240422_5_restr, fitted(z_240422_5_2_restr))
lines(supsmu(time240422_5_restr, res240422_5_restr[,"susinidum_coc"]),lty=2)
title("Sch. res., susinidum_coc")

plot(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out)[which(attr(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out$coefficients, "names") == "susinidum_coc")], lwd=2, main= "Residuals of susinidum_coc")
abline(0,0, col="red", lty=3, lwd=2)
abline(h=model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out$coefficients["susinidum_coc"], col=3, lwd=2, lty= 3)
legend("bottomleft", legend= c("Reference line for the null effect", "Average hazard over time", "Time-varying hazard"), lty=c(3,2,1), col= c("red",3,1), lwd=2, bty = "n", bg = "transparent")

km_susinidum_coc <- survfit(Surv(lag_time,time,event)~ susinidum_coc+ 
    cluster(id), data = data_mine_miss_restr_proc2)
plot(km_susinidum_coc, fun = "cloglog", xlab = "Time (in days) using log",col=c("black", "red"), ylab = "log-log survival", main = "log-log curves by susinidum_coc")
legend("bottomright", legend=c("=0", "=1"), col=c("black", "red"), lty=1, bty = "n", bg = "transparent")

recorded_plot <- recordPlot() 

folder_path <- ifelse(dir.exists("E:/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/"),
                      "E:/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/",
                      "C:/Users/CISS Fondecyt/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/")
png(paste0(folder_path,"zph/proportionallity_susinidum_coc_restr.png"), height=13, width=10, res=500, units="in") 
recorded_plot
dev.off()
pdf 
  2 
Code
invisible("Now with Initial substance: Cocaine")

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
results_susinidum_coc <- data.frame(spec = character(), aic = numeric(), cox_zph = numeric(), success = logical())

for (i in seq(.1,1.5,.025)) {
  tvcs <- c(20,80)*i
  
  data_mine_miss_restr_proc2$susinidum_coc_rec<-
    create_time_varying_effect(data_mine_miss_restr_proc2$susinidum_coc, 
                               data_mine_miss_restr_proc2$time, 
                               tvcs[1],
                               tvcs[2],
                               max(data_mine_miss_restr_proc2$time))
  
  model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc<-
    cph(as.formula(paste0("Surv(lag_time,time,event==1)~ 
            cluster(id)+ 
            lag_tr_outcome_rec +
            log_lag_dias_treat_imp_sin_na +
            lag_less_90d_tr1_rec+
            lag_comp_bpsc_y3_severe_rec + 
            lag_policonsumo2 + 
            edad_al_ing_1 + 
            ano_nac_corr + 
            susinidum_oh +
            susinidum_coc_rec +
            susinidum_pbc +
            susinidum_mar +
            psycom_dum_with +
            psycom_dum_study + 
            freq_cons_dum_5day +
            cond_oc_dum_2inact +
            cond_oc_dum_3unemp +
            susprindum_oh +
            susprindum_coc +
            susprindum_pbc +
            susprindum_mar+
            strat(tipo_de_plan_2_mod)")), 
        data=data_mine_miss_restr_proc2 %>% 
          data.table::as.data.table() %>% data.frame(), 
        x=TRUE, y=TRUE, surv=TRUE, iter.max = 250*4, tol = 1e-6)
  
  cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc)
  invisible("")
  print(paste0("iteration: ",i))
  aic_iter <- round(extractAIC(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc)[[2]],1)
  print( paste0("Time points: ",tvcs[1],", ", tvcs[2]))
  print( paste0("AIC: ", aic_iter))
  cox_zph_iter <- format(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc)$table[nrow(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc)$table), 1])
  print(paste0("Cox ZPH: ", cox_zph_iter))
  
  results_susinidum_coc <- rbind.data.frame(results_susinidum_coc, 
                                             cbind.data.frame(
                                               spec = paste0("iteration: ",i, "; tvcs: ", tvcs[1],", ", tvcs[2]), 
                                               aic = aic_iter,
                                               cox_zph = cox_zph_iter,
                                               success = T
                                             ))
}
[1] "iteration: 0.1"
[1] "Time points: 2, 8"
[1] "AIC: 242541.3"
[1] "Cox ZPH: 475.1056"
[1] "iteration: 0.125"
[1] "Time points: 2.5, 10"
[1] "AIC: 242534.1"
[1] "Cox ZPH: 489.6475"
[1] "iteration: 0.15"
[1] "Time points: 3, 12"
[1] "AIC: 242528.2"
[1] "Cox ZPH: 500.2812"
[1] "iteration: 0.175"
[1] "Time points: 3.5, 14"
[1] "AIC: 242524.4"
[1] "Cox ZPH: 504.4021"
[1] "iteration: 0.2"
[1] "Time points: 4, 16"
[1] "AIC: 242522.7"
[1] "Cox ZPH: 501.8106"
[1] "iteration: 0.225"
[1] "Time points: 4.5, 18"
[1] "AIC: 242523.2"
[1] "Cox ZPH: 492.8092"
[1] "iteration: 0.25"
[1] "Time points: 5, 20"
[1] "AIC: 242525.5"
[1] "Cox ZPH: 479.0295"
[1] "iteration: 0.275"
[1] "Time points: 5.5, 22"
[1] "AIC: 242530.1"
[1] "Cox ZPH: 458.4139"
[1] "iteration: 0.3"
[1] "Time points: 6, 24"
[1] "AIC: 242536.2"
[1] "Cox ZPH: 435.6086"
[1] "iteration: 0.325"
[1] "Time points: 6.5, 26"
[1] "AIC: 242543.6"
[1] "Cox ZPH: 411.411"
[1] "iteration: 0.35"
[1] "Time points: 7, 28"
[1] "AIC: 242553.4"
[1] "Cox ZPH: 384.4102"
[1] "iteration: 0.375"
[1] "Time points: 7.5, 30"
[1] "AIC: 242564.6"
[1] "Cox ZPH: 359.8189"
[1] "iteration: 0.4"
[1] "Time points: 8, 32"
[1] "AIC: 242576.1"
[1] "Cox ZPH: 342.9774"
[1] "iteration: 0.425"
[1] "Time points: 8.5, 34"
[1] "AIC: 242587.3"
[1] "Cox ZPH: 337.1026"
[1] "iteration: 0.45"
[1] "Time points: 9, 36"
[1] "AIC: 242597.7"
[1] "Cox ZPH: 343.4623"
[1] "iteration: 0.475"
[1] "Time points: 9.5, 38"
[1] "AIC: 242607.8"
[1] "Cox ZPH: 362.4096"
[1] "iteration: 0.5"
[1] "Time points: 10, 40"
[1] "AIC: 242617.6"
[1] "Cox ZPH: 394.2462"
[1] "iteration: 0.525"
[1] "Time points: 10.5, 42"
[1] "AIC: 242627.6"
[1] "Cox ZPH: 440.5531"
[1] "iteration: 0.55"
[1] "Time points: 11, 44"
[1] "AIC: 242638.2"
[1] "Cox ZPH: 503.2296"
[1] "iteration: 0.575"
[1] "Time points: 11.5, 46"
[1] "AIC: 242648.6"
[1] "Cox ZPH: 577.8865"
[1] "iteration: 0.6"
[1] "Time points: 12, 48"
[1] "AIC: 242659.1"
[1] "Cox ZPH: 662.8315"
[1] "iteration: 0.625"
[1] "Time points: 12.5, 50"
[1] "AIC: 242669.5"
[1] "Cox ZPH: 753.6261"
[1] "iteration: 0.65"
[1] "Time points: 13, 52"
[1] "AIC: 242679.9"
[1] "Cox ZPH: 846.3598"
[1] "iteration: 0.675"
[1] "Time points: 13.5, 54"
[1] "AIC: 242689.6"
[1] "Cox ZPH: 931.1736"
[1] "iteration: 0.7"
[1] "Time points: 14, 56"
[1] "AIC: 242698.7"
[1] "Cox ZPH: 1007.856"
[1] "iteration: 0.725"
[1] "Time points: 14.5, 58"
[1] "AIC: 242707.1"
[1] "Cox ZPH: 1071.06"
[1] "iteration: 0.75"
[1] "Time points: 15, 60"
[1] "AIC: 242715"
[1] "Cox ZPH: 1120.409"
[1] "iteration: 0.775"
[1] "Time points: 15.5, 62"
[1] "AIC: 242722.2"
[1] "Cox ZPH: 1154.777"
[1] "iteration: 0.8"
[1] "Time points: 16, 64"
[1] "AIC: 242728.3"
[1] "Cox ZPH: 1176.89"
[1] "iteration: 0.825"
[1] "Time points: 16.5, 66"
[1] "AIC: 242733.5"
[1] "Cox ZPH: 1187.06"
[1] "iteration: 0.85"
[1] "Time points: 17, 68"
[1] "AIC: 242737.5"
[1] "Cox ZPH: 1183.687"
[1] "iteration: 0.875"
[1] "Time points: 17.5, 70"
[1] "AIC: 242740.1"
[1] "Cox ZPH: 1169.481"
[1] "iteration: 0.9"
[1] "Time points: 18, 72"
[1] "AIC: 242741.3"
[1] "Cox ZPH: 1145.532"
[1] "iteration: 0.925"
[1] "Time points: 18.5, 74"
[1] "AIC: 242740.9"
[1] "Cox ZPH: 1112.816"
[1] "iteration: 0.95"
[1] "Time points: 19, 76"
[1] "AIC: 242738.7"
[1] "Cox ZPH: 1073.512"
[1] "iteration: 0.975"
[1] "Time points: 19.5, 78"
[1] "AIC: 242734.7"
[1] "Cox ZPH: 1029.364"
[1] "iteration: 1"
[1] "Time points: 20, 80"
[1] "AIC: 242728.6"
[1] "Cox ZPH: 981.5722"
[1] "iteration: 1.025"
[1] "Time points: 20.5, 82"
[1] "AIC: 242720.4"
[1] "Cox ZPH: 932.6265"
[1] "iteration: 1.05"
[1] "Time points: 21, 84"
[1] "AIC: 242709.7"
[1] "Cox ZPH: 881.6735"
[1] "iteration: 1.075"
[1] "Time points: 21.5, 86"
[1] "AIC: 242696.1"
[1] "Cox ZPH: 829.7161"
[1] "iteration: 1.1"
[1] "Time points: 22, 88"
[1] "AIC: 242679.4"
[1] "Cox ZPH: 776.9707"
[1] "iteration: 1.125"
[1] "Time points: 22.5, 90"
[1] "AIC: 242659.1"
[1] "Cox ZPH: 724.111"
[1] "iteration: 1.15"
[1] "Time points: 23, 92"
[1] "AIC: 242634.5"
[1] "Cox ZPH: 670.8339"
[1] "iteration: 1.175"
[1] "Time points: 23.5, 94"
[1] "AIC: 242605.3"
[1] "Cox ZPH: 617.9034"
[1] "iteration: 1.2"
[1] "Time points: 24, 96"
[1] "AIC: 242571.5"
[1] "Cox ZPH: 566.5202"
[1] "iteration: 1.225"
[1] "Time points: 24.5, 98"
[1] "AIC: 242534.1"
[1] "Cox ZPH: 518.4305"
[1] "iteration: 1.25"
[1] "Time points: 25, 100"
[1] "AIC: 242494.1"
[1] "Cox ZPH: 483.4053"
[1] "iteration: 1.275"
[1] "Time points: 25.5, 102"
[1] "AIC: 242453.5"
[1] "Cox ZPH: 449.0626"
[1] "iteration: 1.3"
[1] "Time points: 26, 104"
[1] "AIC: 242423.1"
[1] "Cox ZPH: 428.7996"
[1] "iteration: 1.325"
[1] "Time points: 26.5, 106"
[1] "AIC: 242394.9"
[1] "Cox ZPH: 415.4705"
[1] "iteration: 1.35"
[1] "Time points: 27, 108"
[1] "AIC: 242371.7"
[1] "Cox ZPH: 411.3971"
[1] "iteration: 1.375"
[1] "Time points: 27.5, 110"
[1] "AIC: 242350"
[1] "Cox ZPH: 424.4674"
[1] "iteration: 1.4"
[1] "Time points: 28, 112"
[1] "AIC: 242330.3"
[1] "Cox ZPH: 474.1608"
[1] "iteration: 1.425"
[1] "Time points: 28.5, 114"
[1] "AIC: 242313.6"
[1] "Cox ZPH: 581.5571"
[1] "iteration: 1.45"
[1] "Time points: 29, 116"
[1] "AIC: 242304.5"
[1] "Cox ZPH: 701.7825"
[1] "iteration: 1.475"
[1] "Time points: 29.5, 118"
[1] "AIC: 242301.8"
[1] "Cox ZPH: 757.7131"
[1] "iteration: 1.5"
[1] "Time points: 30, 120"
[1] "AIC: 242301.4"
[1] "Cox ZPH: 769.7678"
Code
results_susinidum_coc[which(as.numeric(results_susinidum_coc$cox_zph)==
                               min(as.numeric(results_susinidum_coc$cox_zph))),]
                              spec      aic  cox_zph success
14 iteration: 0.425; tvcs: 8.5, 34 242587.3 337.1026    TRUE
Code
#                               spec      aic  cox_zph success
# 14 iteration: 0.425; tvcs: 8.5, 34 242575.7 336.8893    TRUE

message(paste0("The previously modified model: ",
               round(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out)$table[nrow(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out)$table), 1],1),
               " vs. the minimum model: ",  results_susinidum_coc[which(as.numeric(results_susinidum_coc$cox_zph)== min(as.numeric(results_susinidum_coc$cox_zph))),"cox_zph"]
))

The previously modified model: 321.7 vs. the minimum model: 337.1026

Code
#The previously modified model: 321.3 vs. the minimum model: 336.8893
invisible("It worsen")

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
require(splines)

specs_susinidum_coc <- paste0("susinidum_coc *", c("time", "log(time+0.001)", "poly(ifelse(is.na(time), .001,time), 3)", "poly(ifelse(is.na(time), .001,time), 4)", "poly(ifelse(is.na(time), .001,time), 5)", "poly(ifelse(is.na(time), .001,time), 6)", "pspline(log(time+0.001), df=3)", "pspline(log(time+0.001), df=4)", "pspline(log(time+0.001), df=5)", "pspline(log(time+0.001), df=6)", "bs(time, 3)", "bs(time, 4)", "bs(time, 5)", "bs(time, 6)", "ns(time, 3)", "ns(time, 4)", "ns(time, 5)", "ns(time, 6)", "rcs(time, 3)", "rcs(time, 4)", "rcs(time, 5)", "rcs(time, 6)"))

results_susini_coc <- data.frame(spec = character(), aic = numeric(), cox_zph = numeric(), success = logical())

for (i in seq_along(specs_susinidum_coc)) {
  l <- specs_susinidum_coc[i]
  
    
  result <- tryCatch({
    model <- cph(as.formula(paste0("Surv(lag_time,time,event==1)~ 
            cluster(id)+ 
            lag_tr_outcome_rec +
            log_lag_dias_treat_imp_sin_na +
            lag_less_90d_tr1_rec+
            lag_comp_bpsc_y3_severe_rec + 
            lag_policonsumo2 + 
            edad_al_ing_1 + 
            ano_nac_corr + 
            susinidum_oh +
            I(round(",l,",2)) +
            susinidum_pbc +
            susinidum_mar +
            psycom_dum_with +
            psycom_dum_study + 
            freq_cons_dum_5day +
            cond_oc_dum_2inact +
            cond_oc_dum_3unemp +
            susprindum_oh +
            susprindum_coc +
            susprindum_pbc +
            susprindum_mar+
            strat(tipo_de_plan_2_mod)")), 
                 data=data_mine_miss_restr_proc2 %>% 
                   dplyr::mutate(log_lag_dias_treat_imp_sin_na_rec = bs(log_lag_dias_treat_imp_sin_na, 3)) %>% 
                   data.table::as.data.table() %>% data.frame(), 
                 x=TRUE, y=TRUE, surv=TRUE, iter.max = 250*4, tol = 1e-6)
    
    aic_val <- extractAIC(model)[[2]]
    cox_zph_val <- cox.zph(model)$table[nrow(cox.zph(model)$table), 1]
    success <- TRUE 
    
  }, error = function(e) {
    aic_val <- NA
    cox_zph_val <- NA
    success <- FALSE
    warning(paste0("Error fitting specification ", i, " (", l, "): ", e))
  })
  
  results_susini_coc <- rbind(results_susini_coc, data.frame(spec = l, aic = aic_val, cox_zph = cox_zph_val, success = success))
  
  print(cat(paste("Iteration:", i, "\n"))) # Add iteration number
  print(cat(paste("Spec: ", l, "\n")))
  print(cat(paste("AIC: ", results_susini_oh$aic[i], "\n"))) 
  print(cat(paste("Cox zph test statistic: ", results_susini_oh$cox_zph[i], "\n")))
}
Iteration: 1 
NULL
Spec:  susinidum_coc *time 
NULL
AIC:  239724.019577566 
NULL
Cox zph test statistic:  4672.22758383257 
NULL
Iteration: 2 
NULL
Spec:  susinidum_coc *log(time+0.001) 
NULL
AIC:  241585.150871146 
NULL
Cox zph test statistic:  1204.10177494143 
NULL
Iteration: 3 
NULL
Spec:  susinidum_coc *poly(ifelse(is.na(time), .001,time), 3) 
NULL
AIC:  240219.630415016 
NULL
Cox zph test statistic:  7926.39955359493 
NULL
Iteration: 4 
NULL
Spec:  susinidum_coc *poly(ifelse(is.na(time), .001,time), 4) 
NULL
AIC:  240063.039259188 
NULL
Cox zph test statistic:  8212.21889149851 
NULL
Iteration: 5 
NULL
Spec:  susinidum_coc *poly(ifelse(is.na(time), .001,time), 5) 
NULL
AIC:  240016.095427946 
NULL
Cox zph test statistic:  8304.47329075385 
NULL
Iteration: 6 
NULL
Spec:  susinidum_coc *poly(ifelse(is.na(time), .001,time), 6) 
NULL
AIC:  239508.178249717 
NULL
Cox zph test statistic:  8101.01468172362 
NULL
Iteration: 7 
NULL
Spec:  susinidum_coc *pspline(log(time+0.001), df=3) 
NULL
AIC:  239508.178249717 
NULL
Cox zph test statistic:  8101.01468172362 
NULL
Iteration: 8 
NULL
Spec:  susinidum_coc *pspline(log(time+0.001), df=4) 
NULL
AIC:  239508.178249717 
NULL
Cox zph test statistic:  8101.01468172362 
NULL
Iteration: 9 
NULL
Spec:  susinidum_coc *pspline(log(time+0.001), df=5) 
NULL
AIC:  239508.178249717 
NULL
Cox zph test statistic:  8101.01468172362 
NULL
Iteration: 10 
NULL
Spec:  susinidum_coc *pspline(log(time+0.001), df=6) 
NULL
AIC:  239508.178249717 
NULL
Cox zph test statistic:  8101.01468172362 
NULL
Iteration: 11 
NULL
Spec:  susinidum_coc *bs(time, 3) 
NULL
AIC:  239518.026724213 
NULL
Cox zph test statistic:  10331.3482921498 
NULL
Iteration: 12 
NULL
Spec:  susinidum_coc *bs(time, 4) 
NULL
AIC:  236772.640457164 
NULL
Cox zph test statistic:  18133.928243881 
NULL
Iteration: 13 
NULL
Spec:  susinidum_coc *bs(time, 5) 
NULL
AIC:  235970.965928735 
NULL
Cox zph test statistic:  20900.8309746657 
NULL
Iteration: 14 
NULL
Spec:  susinidum_coc *bs(time, 6) 
NULL
AIC:  235677.262806318 
NULL
Cox zph test statistic:  21727.8304904265 
NULL
Iteration: 15 
NULL
Spec:  susinidum_coc *ns(time, 3) 
NULL
AIC:  238253.396508616 
NULL
Cox zph test statistic:  14728.7767838593 
NULL
Iteration: 16 
NULL
Spec:  susinidum_coc *ns(time, 4) 
NULL
AIC:  236653.226858842 
NULL
Cox zph test statistic:  19308.1808541226 
NULL
Iteration: 17 
NULL
Spec:  susinidum_coc *ns(time, 5) 
NULL
AIC:  236251.069626771 
NULL
Cox zph test statistic:  20993.031671471 
NULL
Iteration: 18 
NULL
Spec:  susinidum_coc *ns(time, 6) 
NULL
AIC:  235896.939555169 
NULL
Cox zph test statistic:  21101.9090902985 
NULL
Iteration: 19 
NULL
Spec:  susinidum_coc *rcs(time, 3) 
NULL
AIC:  239671.217452014 
NULL
Cox zph test statistic:  9313.41322424369 
NULL
Iteration: 20 
NULL
Spec:  susinidum_coc *rcs(time, 4) 
NULL
AIC:  239671.217452014 
NULL
Cox zph test statistic:  9313.41322424369 
NULL
Iteration: 21 
NULL
Spec:  susinidum_coc *rcs(time, 5) 
NULL
AIC:  239671.217452014 
NULL
Cox zph test statistic:  9313.41322424369 
NULL
X matrix deemed to be singular; variable time''' 
Iteration: 22 
NULL
Spec:  susinidum_coc *rcs(time, 6) 
NULL
AIC:  239671.217452014 
NULL
Cox zph test statistic:  9313.41322424369 
NULL
Code
results_susini_coc[which(as.numeric(results_susini_coc$cox_zph)==
                           min(as.numeric(results_susini_coc$cox_zph))),]
                            spec      aic  cox_zph success
2 susinidum_coc *log(time+0.001) 242707.4 323.3394    TRUE
Code
#                             spec      aic cox_zph success
# 2 susinidum_coc *log(time+0.001) 242695.6 323.038    TRUE
invisible("It worsen. We do nothing")

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
results_susinidum_coc_alt <- data.frame(spec = character(), aic = numeric(), cox_zph = numeric(), success = logical())

for (i in seq(1,5,.1)) {
  change_points <- c(5*i, 10*i, 15*i)  # Example change points (months)
  coefficients <- c(0.5, 1.5, 0.5, 1)  # Example coefficients
  # Add the time-varying coefficient to your model
  
  data_mine_miss_restr_proc2$susinidum_coc_rec2 <- with(data_mine_miss_restr_proc2,
                                                       susinidum_coc*time_varying_coeff(time, change_points, coefficients))

  model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc_alt<-
    cph(as.formula(paste0("Surv(lag_time,time,event==1)~ 
            cluster(id)+ 
            lag_tr_outcome_rec +
            log_lag_dias_treat_imp_sin_na +
            lag_less_90d_tr1_rec+
            lag_comp_bpsc_y3_severe_rec + 
            lag_policonsumo2 + 
            edad_al_ing_1 + 
            ano_nac_corr + 
            susinidum_coc_rec2 +
            susinidum_oh +
            susinidum_pbc +
            susinidum_mar +
            psycom_dum_with +
            psycom_dum_study + 
            freq_cons_dum_5day +
            cond_oc_dum_2inact +
            cond_oc_dum_3unemp +
            susprindum_oh +
            susprindum_coc +
            susprindum_pbc +
            susprindum_mar+
            strat(tipo_de_plan_2_mod)")), 
        data=data_mine_miss_restr_proc2 %>% 
          data.table::as.data.table() %>% data.frame(), 
        x=TRUE, y=TRUE, surv=TRUE, iter.max = 250*4, tol = 1e-6)
  
  cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc_alt)
  invisible("")
  print(paste0("iteration: ",i))
  aic_iter <- round(extractAIC(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc_alt)[[2]],1)
  
  print( paste0("Time points: ",change_points[1],", ", change_points[2], ", ",change_points[3]))
  print( paste0("AIC: ", aic_iter))
  cox_zph_iter <- format(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc_alt)$table[nrow(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc_alt)$table), 1])
  print(paste0("Cox ZPH: ", cox_zph_iter))
  
  results_susinidum_coc_alt <- rbind.data.frame(results_susinidum_coc_alt, 
                                                 cbind.data.frame(
                                                   spec = paste0("iteration: ",i, "; change points: ", change_points[1],", ", change_points[2], ", ",change_points[3]), 
                                                   aic = aic_iter,
                                                   cox_zph = cox_zph_iter,
                                                   success = T
                                                 ))
}
[1] "iteration: 1"
[1] "Time points: 5, 10, 15"
[1] "AIC: 242740.9"
[1] "Cox ZPH: 318.8496"
[1] "iteration: 1.1"
[1] "Time points: 5.5, 11, 16.5"
[1] "AIC: 242740.9"
[1] "Cox ZPH: 319.5982"
[1] "iteration: 1.2"
[1] "Time points: 6, 12, 18"
[1] "AIC: 242741"
[1] "Cox ZPH: 321.6762"
[1] "iteration: 1.3"
[1] "Time points: 6.5, 13, 19.5"
[1] "AIC: 242740.9"
[1] "Cox ZPH: 321.3254"
[1] "iteration: 1.4"
[1] "Time points: 7, 14, 21"
[1] "AIC: 242740.8"
[1] "Cox ZPH: 321.5408"
[1] "iteration: 1.5"
[1] "Time points: 7.5, 15, 22.5"
[1] "AIC: 242740.6"
[1] "Cox ZPH: 322.0213"
[1] "iteration: 1.6"
[1] "Time points: 8, 16, 24"
[1] "AIC: 242741"
[1] "Cox ZPH: 324.2359"
[1] "iteration: 1.7"
[1] "Time points: 8.5, 17, 25.5"
[1] "AIC: 242741.3"
[1] "Cox ZPH: 327.4271"
[1] "iteration: 1.8"
[1] "Time points: 9, 18, 27"
[1] "AIC: 242741.3"
[1] "Cox ZPH: 327.9855"
[1] "iteration: 1.9"
[1] "Time points: 9.5, 19, 28.5"
[1] "AIC: 242741.3"
[1] "Cox ZPH: 328.6902"
[1] "iteration: 2"
[1] "Time points: 10, 20, 30"
[1] "AIC: 242741.1"
[1] "Cox ZPH: 329.7676"
[1] "iteration: 2.1"
[1] "Time points: 10.5, 21, 31.5"
[1] "AIC: 242741"
[1] "Cox ZPH: 327.3083"
[1] "iteration: 2.2"
[1] "Time points: 11, 22, 33"
[1] "AIC: 242741"
[1] "Cox ZPH: 325.072"
[1] "iteration: 2.3"
[1] "Time points: 11.5, 23, 34.5"
[1] "AIC: 242740.3"
[1] "Cox ZPH: 324.9401"
[1] "iteration: 2.4"
[1] "Time points: 12, 24, 36"
[1] "AIC: 242739.6"
[1] "Cox ZPH: 323.5421"
[1] "iteration: 2.5"
[1] "Time points: 12.5, 25, 37.5"
[1] "AIC: 242740.1"
[1] "Cox ZPH: 321.4559"
[1] "iteration: 2.6"
[1] "Time points: 13, 26, 39"
[1] "AIC: 242739.5"
[1] "Cox ZPH: 319.2246"
[1] "iteration: 2.7"
[1] "Time points: 13.5, 27, 40.5"
[1] "AIC: 242739"
[1] "Cox ZPH: 318.7078"
[1] "iteration: 2.8"
[1] "Time points: 14, 28, 42"
[1] "AIC: 242738.8"
[1] "Cox ZPH: 317.9917"
[1] "iteration: 2.9"
[1] "Time points: 14.5, 29, 43.5"
[1] "AIC: 242739.3"
[1] "Cox ZPH: 317.7749"
[1] "iteration: 3"
[1] "Time points: 15, 30, 45"
[1] "AIC: 242740.1"
[1] "Cox ZPH: 317.7125"
[1] "iteration: 3.1"
[1] "Time points: 15.5, 31, 46.5"
[1] "AIC: 242740.4"
[1] "Cox ZPH: 317.9962"
[1] "iteration: 3.2"
[1] "Time points: 16, 32, 48"
[1] "AIC: 242741.2"
[1] "Cox ZPH: 319.1036"
[1] "iteration: 3.3"
[1] "Time points: 16.5, 33, 49.5"
[1] "AIC: 242741.3"
[1] "Cox ZPH: 320.6839"
[1] "iteration: 3.4"
[1] "Time points: 17, 34, 51"
[1] "AIC: 242741.3"
[1] "Cox ZPH: 323.1721"
[1] "iteration: 3.5"
[1] "Time points: 17.5, 35, 52.5"
[1] "AIC: 242740.6"
[1] "Cox ZPH: 326.611"
[1] "iteration: 3.6"
[1] "Time points: 18, 36, 54"
[1] "AIC: 242740.6"
[1] "Cox ZPH: 328.6784"
[1] "iteration: 3.7"
[1] "Time points: 18.5, 37, 55.5"
[1] "AIC: 242740"
[1] "Cox ZPH: 333.0428"
[1] "iteration: 3.8"
[1] "Time points: 19, 38, 57"
[1] "AIC: 242739.3"
[1] "Cox ZPH: 336.2208"
[1] "iteration: 3.9"
[1] "Time points: 19.5, 39, 58.5"
[1] "AIC: 242738.4"
[1] "Cox ZPH: 339.4407"
[1] "iteration: 4"
[1] "Time points: 20, 40, 60"
[1] "AIC: 242737.2"
[1] "Cox ZPH: 344.0601"
[1] "iteration: 4.1"
[1] "Time points: 20.5, 41, 61.5"
[1] "AIC: 242735.7"
[1] "Cox ZPH: 347.9692"
[1] "iteration: 4.2"
[1] "Time points: 21, 42, 63"
[1] "AIC: 242735"
[1] "Cox ZPH: 352.4328"
[1] "iteration: 4.3"
[1] "Time points: 21.5, 43, 64.5"
[1] "AIC: 242734.3"
[1] "Cox ZPH: 357.9312"
[1] "iteration: 4.4"
[1] "Time points: 22, 44, 66"
[1] "AIC: 242732.4"
[1] "Cox ZPH: 359.4456"
[1] "iteration: 4.5"
[1] "Time points: 22.5, 45, 67.5"
[1] "AIC: 242731"
[1] "Cox ZPH: 364.9348"
[1] "iteration: 4.6"
[1] "Time points: 23, 46, 69"
[1] "AIC: 242730.3"
[1] "Cox ZPH: 365.0121"
[1] "iteration: 4.7"
[1] "Time points: 23.5, 47, 70.5"
[1] "AIC: 242729.2"
[1] "Cox ZPH: 363.3641"
[1] "iteration: 4.8"
[1] "Time points: 24, 48, 72"
[1] "AIC: 242727.3"
[1] "Cox ZPH: 367.802"
[1] "iteration: 4.9"
[1] "Time points: 24.5, 49, 73.5"
[1] "AIC: 242728.2"
[1] "Cox ZPH: 370.3726"
[1] "iteration: 5"
[1] "Time points: 25, 50, 75"
[1] "AIC: 242727.4"
[1] "Cox ZPH: 370.8872"
Code
results_susinidum_coc_alt[which(as.numeric(results_susinidum_coc_alt$cox_zph)==
                                   min(as.numeric(results_susinidum_coc_alt$cox_zph))),]
                                      spec      aic  cox_zph success
21 iteration: 3; change points: 15, 30, 45 242740.1 317.7125    TRUE
Code
#                                       spec      aic  cox_zph success
# 21 iteration: 3; change points: 15, 30, 45 242728.3 317.3692    TRUE

message(paste0("The minimum of the first modified model: ",
               format(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out)$table[nrow(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out)$table), 1]),
               " vs. the minimum model: ",  results_susinidum_coc_alt[which(as.numeric(results_susinidum_coc_alt$cox_zph)== min(as.numeric(results_susinidum_coc_alt$cox_zph))),"cox_zph"]
))

The minimum of the first modified model: 321.6527 vs. the minimum model: 317.7125

Code
#The minimum of the first modified model: 321.3004 vs. the minimum model: 317.3692
invisible("It improves")

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
results_susinidum_coc_alt[which(as.numeric(results_susinidum_coc_alt$cox_zph)==
                                   min(as.numeric(results_susinidum_coc_alt$cox_zph))),]
                                      spec      aic  cox_zph success
21 iteration: 3; change points: 15, 30, 45 242740.1 317.7125    TRUE
Code
change_points <- c(5*3, 10*3, 15*3)  # Example change points (months)
coefficients <- c(0.5, 1.5, 0.5, 1)  # Example coefficients
# Add the time-varying coefficient to your model

data_mine_miss_restr_proc2$susinidum_coc_rec2 <- with(data_mine_miss_restr_proc2,
                                                     susinidum_coc*time_varying_coeff(time, change_points, coefficients))

model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc_alt<-
  cph(Surv(lag_time,time,event==1)~ 
            cluster(id)+ 
            lag_tr_outcome_rec +
            log_lag_dias_treat_imp_sin_na +
            lag_less_90d_tr1_rec+
            lag_comp_bpsc_y3_severe_rec + 
            lag_policonsumo2 + 
            edad_al_ing_1 + 
            ano_nac_corr + 
            susinidum_coc_rec2 +
            susinidum_oh +
            susinidum_pbc +
            susinidum_mar +
            psycom_dum_with +
            psycom_dum_study + 
            freq_cons_dum_5day +
            cond_oc_dum_2inact +
            cond_oc_dum_3unemp +
            susprindum_oh +
            susprindum_coc +
            susprindum_pbc +
            susprindum_mar+
            strat(tipo_de_plan_2_mod), 
      data=data_mine_miss_restr_proc2 %>% 
        data.table::as.data.table() %>% data.frame(), 
      x=TRUE, y=TRUE, surv=TRUE, iter.max = 250*4, tol = 1e-6)

round(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc_alt)$table,4)
                                 chisq df      p
lag_tr_outcome_rec             66.6088  1 0.0000
log_lag_dias_treat_imp_sin_na  10.5341  1 0.0012
lag_less_90d_tr1_rec           84.1713  1 0.0000
lag_comp_bpsc_y3_severe_rec    17.3155  1 0.0000
lag_policonsumo2                3.2002  1 0.0736
edad_al_ing_1                   0.1512  1 0.6974
ano_nac_corr                    5.1470  1 0.0233
susinidum_coc_rec2              2.9483  1 0.0860
susinidum_oh                   19.9650  1 0.0000
susinidum_pbc                   2.0948  1 0.1478
susinidum_mar                   6.7931  1 0.0092
psycom_dum_with                11.7373  1 0.0006
psycom_dum_study                1.6233  1 0.2026
freq_cons_dum_5day              0.0825  1 0.7739
cond_oc_dum_2inact              4.4758  1 0.0344
cond_oc_dum_3unemp              1.7447  1 0.1865
susprindum_oh                   4.3698  1 0.0366
susprindum_coc                  1.5662  1 0.2108
susprindum_pbc                  0.0503  1 0.8226
susprindum_mar                  5.2711  1 0.0217
GLOBAL                        317.7125 20 0.0000
Code
#                                  chisq df      p
# lag_tr_outcome_rec             66.7157  1 0.0000
# log_lag_dias_treat_imp_sin_na  10.3761  1 0.0013
# lag_less_90d_tr1_rec           84.1743  1 0.0000
# lag_comp_bpsc_y3_severe_rec    17.2693  1 0.0000
# lag_policonsumo2                3.1689  1 0.0751
# edad_al_ing_1                   0.1432  1 0.7051
# ano_nac_corr                    5.1839  1 0.0228
# susinidum_coc_rec2              2.9377  1 0.0865
# susinidum_oh                   19.7620  1 0.0000
# susinidum_pbc                   2.0158  1 0.1557
# susinidum_mar                   6.7590  1 0.0093
# psycom_dum_with                11.7916  1 0.0006
# psycom_dum_study                1.6349  1 0.2010
# freq_cons_dum_5day              0.0541  1 0.8161
# cond_oc_dum_2inact              4.5002  1 0.0339
# cond_oc_dum_3unemp              1.6255  1 0.2023
# susprindum_oh                   4.2826  1 0.0385
# susprindum_coc                  1.6036  1 0.2054
# susprindum_pbc                  0.0666  1 0.7964
# susprindum_mar                  5.3207  1 0.0211
# GLOBAL                        317.3692 20 0.0000

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
invisible("Analyse the violation of proportional hazards")
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
try(dev.off())
null device 
          1 
Code
res240422_6_restr <- resid(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc_alt, "scaledsch")
time240422_6_restr <- as.numeric(dimnames(res240422_6_restr)[[1]])

z_240422_6_1_restr <- loess(res240422_6_restr[,"susinidum_oh"] ~ time240422_6_restr, span=0.50)   # residuals 

par(mfrow= c(3,1))
plot(time240422_6_restr, fitted(z_240422_6_1_restr))
lines(supsmu(time240422_6_restr, res240422_6_restr[,"susinidum_oh"]),lty=2)
title("Sch. res., susinidum_coc")

plot(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc_alt)[which(attr(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc_alt$coefficients, "names") == "susinidum_oh")], lwd=2, main= "Residuals of susinidum_oh")
abline(0,0, col="red", lty=3, lwd=2)
abline(h=model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc_alt$coefficients["susinidum_oh"], col=3, lwd=2, lty= 3)
legend("bottomleft", legend= c("Reference line for the null effect", "Average hazard over time", "Time-varying hazard"), lty=c(3,2,1), col= c("red",3,1), lwd=2, bty = "n", bg = "transparent")

km_susinidum_oh <- survfit(Surv(lag_time,time,event)~ susinidum_oh+ 
    cluster(id), data = data_mine_miss_restr_proc2)
plot(km_susinidum_oh, fun = "cloglog", xlab = "Time (in days) using log",col=c("black", "red"), ylab = "log-log survival", main = "log-log curves by susinidum_oh")
legend("bottomright", legend=c("=0", "=1"), col=c("black", "red"), lty=1, bty = "n", bg = "transparent")

recorded_plot <- recordPlot() 

folder_path <- ifelse(dir.exists("E:/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/"),
                      "E:/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/",
                      "C:/Users/CISS Fondecyt/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/")
png(paste0(folder_path,"zph/proportionallity_susinidum_oh2_restr.png"), height=13, width=10, res=500, units="in") 
recorded_plot
dev.off()
pdf 
  2 
Code
invisible("Now with Initial substance: Alcohol AGAIN")

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
results_susinidum_oh2 <- data.frame(spec = character(), aic = numeric(), cox_zph = numeric(), success = logical())

for (i in seq(.1,1.5,.025)) {
  tvcs <- c(20,80)*i
  
  data_mine_miss_restr_proc2$susinidum_oh_rec<-
    create_time_varying_effect(data_mine_miss_restr_proc2$susinidum_oh, 
                               data_mine_miss_restr_proc2$time, 
                               tvcs[1],
                               tvcs[2],
                               max(data_mine_miss_restr_proc2$time))
  model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc_alt_susinidum_oh<-
    cph(as.formula(paste0("Surv(lag_time,time,event==1)~ 
            cluster(id)+ 
            lag_tr_outcome_rec +
            log_lag_dias_treat_imp_sin_na +
            lag_less_90d_tr1_rec+
            lag_comp_bpsc_y3_severe_rec + 
            lag_policonsumo2 + 
            edad_al_ing_1 + 
            ano_nac_corr + 
            susinidum_coc_rec2 +
            susinidum_oh_rec +
            susinidum_pbc +
            susinidum_mar +
            psycom_dum_with +
            psycom_dum_study + 
            freq_cons_dum_5day +
            cond_oc_dum_2inact +
            cond_oc_dum_3unemp +
            susprindum_oh +
            susprindum_coc +
            susprindum_pbc +
            susprindum_mar+
            strat(tipo_de_plan_2_mod)")), 
        data=data_mine_miss_restr_proc2 %>% 
          data.table::as.data.table() %>% data.frame(), 
        x=TRUE, y=TRUE, surv=TRUE, iter.max = 250*4, tol = 1e-6)
  
  cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc_alt_susinidum_oh)
  invisible("")
  print(paste0("iteration: ",i))
  aic_iter <- round(extractAIC(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc_alt_susinidum_oh)[[2]],1)
  print( paste0("Time points: ",tvcs[1],", ", tvcs[2]))
  print( paste0("AIC: ", aic_iter))
  cox_zph_iter <- format(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc_alt_susinidum_oh)$table[nrow(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc_alt_susinidum_oh)$table), 1])
  print(paste0("Cox ZPH: ", cox_zph_iter))
  
  results_susinidum_oh2 <- rbind.data.frame(results_susinidum_oh2, 
                                             cbind.data.frame(
                                               spec = paste0("iteration: ",i, "; tvcs: ", tvcs[1],", ", tvcs[2]), 
                                               aic = aic_iter,
                                               cox_zph = cox_zph_iter,
                                               success = T
                                             ))
}
[1] "iteration: 0.1"
[1] "Time points: 2, 8"
[1] "AIC: 239556.7"
[1] "Cox ZPH: 5469.566"
[1] "iteration: 0.125"
[1] "Time points: 2.5, 10"
[1] "AIC: 239513.6"
[1] "Cox ZPH: 5660.287"
[1] "iteration: 0.15"
[1] "Time points: 3, 12"
[1] "AIC: 239499.1"
[1] "Cox ZPH: 5766.978"
[1] "iteration: 0.175"
[1] "Time points: 3.5, 14"
[1] "AIC: 239512.2"
[1] "Cox ZPH: 5799.257"
[1] "iteration: 0.2"
[1] "Time points: 4, 16"
[1] "AIC: 239556.4"
[1] "Cox ZPH: 5748.393"
[1] "iteration: 0.225"
[1] "Time points: 4.5, 18"
[1] "AIC: 239623.7"
[1] "Cox ZPH: 5636.161"
[1] "iteration: 0.25"
[1] "Time points: 5, 20"
[1] "AIC: 239711.5"
[1] "Cox ZPH: 5463.947"
[1] "iteration: 0.275"
[1] "Time points: 5.5, 22"
[1] "AIC: 239820.5"
[1] "Cox ZPH: 5222.299"
[1] "iteration: 0.3"
[1] "Time points: 6, 24"
[1] "AIC: 239953.8"
[1] "Cox ZPH: 4892.01"
[1] "iteration: 0.325"
[1] "Time points: 6.5, 26"
[1] "AIC: 240100.8"
[1] "Cox ZPH: 4497.324"
[1] "iteration: 0.35"
[1] "Time points: 7, 28"
[1] "AIC: 240262.6"
[1] "Cox ZPH: 4030.255"
[1] "iteration: 0.375"
[1] "Time points: 7.5, 30"
[1] "AIC: 240431.9"
[1] "Cox ZPH: 3523.814"
[1] "iteration: 0.4"
[1] "Time points: 8, 32"
[1] "AIC: 240604.4"
[1] "Cox ZPH: 3007.152"
[1] "iteration: 0.425"
[1] "Time points: 8.5, 34"
[1] "AIC: 240780.5"
[1] "Cox ZPH: 2499.49"
[1] "iteration: 0.45"
[1] "Time points: 9, 36"
[1] "AIC: 240959.3"
[1] "Cox ZPH: 2042.524"
[1] "iteration: 0.475"
[1] "Time points: 9.5, 38"
[1] "AIC: 241138"
[1] "Cox ZPH: 1695.105"
[1] "iteration: 0.5"
[1] "Time points: 10, 40"
[1] "AIC: 241309.6"
[1] "Cox ZPH: 1516.189"
[1] "iteration: 0.525"
[1] "Time points: 10.5, 42"
[1] "AIC: 241477.7"
[1] "Cox ZPH: 1530.988"
[1] "iteration: 0.55"
[1] "Time points: 11, 44"
[1] "AIC: 241642.2"
[1] "Cox ZPH: 1754.078"
[1] "iteration: 0.575"
[1] "Time points: 11.5, 46"
[1] "AIC: 241800.2"
[1] "Cox ZPH: 2173.52"
[1] "iteration: 0.6"
[1] "Time points: 12, 48"
[1] "AIC: 241950.1"
[1] "Cox ZPH: 2751.925"
[1] "iteration: 0.625"
[1] "Time points: 12.5, 50"
[1] "AIC: 242092.6"
[1] "Cox ZPH: 3446.823"
[1] "iteration: 0.65"
[1] "Time points: 13, 52"
[1] "AIC: 242227"
[1] "Cox ZPH: 4199.34"
[1] "iteration: 0.675"
[1] "Time points: 13.5, 54"
[1] "AIC: 242350.7"
[1] "Cox ZPH: 4944.467"
[1] "iteration: 0.7"
[1] "Time points: 14, 56"
[1] "AIC: 242460.7"
[1] "Cox ZPH: 5620.455"
[1] "iteration: 0.725"
[1] "Time points: 14.5, 58"
[1] "AIC: 242554.3"
[1] "Cox ZPH: 6178.287"
[1] "iteration: 0.75"
[1] "Time points: 15, 60"
[1] "AIC: 242630.7"
[1] "Cox ZPH: 6598.576"
[1] "iteration: 0.775"
[1] "Time points: 15.5, 62"
[1] "AIC: 242688.3"
[1] "Cox ZPH: 6868.943"
[1] "iteration: 0.8"
[1] "Time points: 16, 64"
[1] "AIC: 242725.3"
[1] "Cox ZPH: 6996.218"
[1] "iteration: 0.825"
[1] "Time points: 16.5, 66"
[1] "AIC: 242740.2"
[1] "Cox ZPH: 7000.32"
[1] "iteration: 0.85"
[1] "Time points: 17, 68"
[1] "AIC: 242732.2"
[1] "Cox ZPH: 6903.379"
[1] "iteration: 0.875"
[1] "Time points: 17.5, 70"
[1] "AIC: 242700"
[1] "Cox ZPH: 6727.182"
[1] "iteration: 0.9"
[1] "Time points: 18, 72"
[1] "AIC: 242643.3"
[1] "Cox ZPH: 6493.766"
[1] "iteration: 0.925"
[1] "Time points: 18.5, 74"
[1] "AIC: 242562.9"
[1] "Cox ZPH: 6225.363"
[1] "iteration: 0.95"
[1] "Time points: 19, 76"
[1] "AIC: 242458.8"
[1] "Cox ZPH: 5933.628"
[1] "iteration: 0.975"
[1] "Time points: 19.5, 78"
[1] "AIC: 242330.8"
[1] "Cox ZPH: 5629.225"
[1] "iteration: 1"
[1] "Time points: 20, 80"
[1] "AIC: 242178"
[1] "Cox ZPH: 5315.827"
[1] "iteration: 1.025"
[1] "Time points: 20.5, 82"
[1] "AIC: 242001.4"
[1] "Cox ZPH: 5006.006"
[1] "iteration: 1.05"
[1] "Time points: 21, 84"
[1] "AIC: 241803.4"
[1] "Cox ZPH: 4708.628"
[1] "iteration: 1.075"
[1] "Time points: 21.5, 86"
[1] "AIC: 241588.2"
[1] "Cox ZPH: 4429.636"
[1] "iteration: 1.1"
[1] "Time points: 22, 88"
[1] "AIC: 241355.7"
[1] "Cox ZPH: 4166.516"
[1] "iteration: 1.125"
[1] "Time points: 22.5, 90"
[1] "AIC: 241106.1"
[1] "Cox ZPH: 3917.886"
[1] "iteration: 1.15"
[1] "Time points: 23, 92"
[1] "AIC: 240845.4"
[1] "Cox ZPH: 3688.558"
[1] "iteration: 1.175"
[1] "Time points: 23.5, 94"
[1] "AIC: 240574.7"
[1] "Cox ZPH: 3476.06"
[1] "iteration: 1.2"
[1] "Time points: 24, 96"
[1] "AIC: 240294.8"
[1] "Cox ZPH: 3285.221"
[1] "iteration: 1.225"
[1] "Time points: 24.5, 98"
[1] "AIC: 240013.4"
[1] "Cox ZPH: 3118.936"
[1] "iteration: 1.25"
[1] "Time points: 25, 100"
[1] "AIC: 239734.8"
[1] "Cox ZPH: 2977.732"
[1] "iteration: 1.275"
[1] "Time points: 25.5, 102"
[1] "AIC: 239459.9"
[1] "Cox ZPH: 2862.778"
[1] "iteration: 1.3"
[1] "Time points: 26, 104"
[1] "AIC: 239188.1"
[1] "Cox ZPH: 2772.228"
[1] "iteration: 1.325"
[1] "Time points: 26.5, 106"
[1] "AIC: 238923.8"
[1] "Cox ZPH: 2710.248"
[1] "iteration: 1.35"
[1] "Time points: 27, 108"
[1] "AIC: 238664.5"
[1] "Cox ZPH: 2681.074"
[1] "iteration: 1.375"
[1] "Time points: 27.5, 110"
[1] "AIC: 238411.7"
[1] "Cox ZPH: 2689.043"
[1] "iteration: 1.4"
[1] "Time points: 28, 112"
[1] "AIC: 238178.4"
[1] "Cox ZPH: 2739.046"
[1] "iteration: 1.425"
[1] "Time points: 28.5, 114"
[1] "AIC: 237964.8"
[1] "Cox ZPH: 2841.23"
[1] "iteration: 1.45"
[1] "Time points: 29, 116"
[1] "AIC: 237767.9"
[1] "Cox ZPH: 3011.975"
[1] "iteration: 1.475"
[1] "Time points: 29.5, 118"
[1] "AIC: 237594.5"
[1] "Cox ZPH: 3274.361"
[1] "iteration: 1.5"
[1] "Time points: 30, 120"
[1] "AIC: 237451.1"
[1] "Cox ZPH: 3701.742"
Code
results_susinidum_oh2[which(as.numeric(results_susinidum_oh2$cox_zph)==
                               min(as.numeric(results_susinidum_oh2$cox_zph))),]
                           spec      aic  cox_zph success
17 iteration: 0.5; tvcs: 10, 40 241309.6 1516.189    TRUE
Code
#                            spec      aic  cox_zph success
# 15 iteration: 0.45; tvcs: 9, 36 242729.5 321.3004    TRUE

results_susinidum_coc_alt[which(as.numeric(results_susinidum_coc_alt$cox_zph)==
                                    min(as.numeric(results_susinidum_coc_alt$cox_zph))),"cox_zph"]
[1] "317.7125"
Code
message(paste0("The previously modified model: ",
               round(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc_alt)$table[nrow(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc_alt)$table), 1],1),
               " vs. the minimum model: ",  results_susinidum_oh2[which(as.numeric(results_susinidum_oh2$cox_zph)== min(as.numeric(results_susinidum_oh2$cox_zph))),"cox_zph"]
))

The previously modified model: 317.7 vs. the minimum model: 1516.189

Code
#The previously modified model: 326.3 vs. the minimum model: 1514.375
invisible("It worsen")

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
require(splines)

specs_susinidum_oh <- paste0("susinidum_oh *", c("time", "log(time+0.001)", "poly(ifelse(is.na(time), .001,time), 3)", "poly(ifelse(is.na(time), .001,time), 4)", "poly(ifelse(is.na(time), .001,time), 5)", "poly(ifelse(is.na(time), .001,time), 6)", "pspline(log(time+0.001), df=3)", "pspline(log(time+0.001), df=4)", "pspline(log(time+0.001), df=5)", "pspline(log(time+0.001), df=6)", "bs(time, 3)", "bs(time, 4)", "bs(time, 5)", "bs(time, 6)", "ns(time, 3)", "ns(time, 4)", "ns(time, 5)", "ns(time, 6)", "rcs(time, 3)", "rcs(time, 4)", "rcs(time, 5)", "rcs(time, 6)"))

results_susini_oh2 <- data.frame(spec = character(), aic = numeric(), cox_zph = numeric(), success = logical())

for (i in seq_along(specs_susinidum_oh)) {
  l <- specs_susinidum_oh[i]
   
  result <- tryCatch({
    model <- cph(as.formula(paste0("Surv(lag_time,time,event==1)~ 
            cluster(id)+ 
            lag_tr_outcome_rec +
            log_lag_dias_treat_imp_sin_na +
            lag_less_90d_tr1_rec+
            lag_comp_bpsc_y3_severe_rec + 
            lag_policonsumo2 + 
            edad_al_ing_1 + 
            ano_nac_corr + 
            susinidum_coc_rec2 +
            I(round(",l,",2)) +
            susinidum_pbc +
            susinidum_mar +
            psycom_dum_with +
            psycom_dum_study + 
            freq_cons_dum_5day +
            cond_oc_dum_2inact +
            cond_oc_dum_3unemp +
            susprindum_oh +
            susprindum_coc +
            susprindum_pbc +
            susprindum_mar+
            strat(tipo_de_plan_2_mod)")), 
                 data=data_mine_miss_restr_proc2 %>% 
                   dplyr::mutate(log_lag_dias_treat_imp_sin_na_rec = bs(log_lag_dias_treat_imp_sin_na, 3)) %>% 
                   data.table::as.data.table() %>% data.frame(), 
                 x=TRUE, y=TRUE, surv=TRUE, iter.max = 250*4, tol = 1e-6)
    
    aic_val <- extractAIC(model)[[2]]
    cox_zph_val <- cox.zph(model)$table[nrow(cox.zph(model)$table), 1]
    success <- TRUE 
    
  }, error = function(e) {
    aic_val <- NA
    cox_zph_val <- NA
    success <- FALSE
    warning(paste0("Error fitting specification ", i, " (", l, "): ", e))
  })
  
  results_susini_oh2 <- rbind(results_susini_oh2, data.frame(spec = l, aic = aic_val, cox_zph = cox_zph_val, success = success))
  
  print(cat(paste("Iteration:", i, "\n"))) # Add iteration number
  print(cat(paste("Spec: ", l, "\n")))
  print(cat(paste("AIC: ", results_susini_oh$aic[i], "\n"))) 
  print(cat(paste("Cox zph test statistic: ", results_susini_oh$cox_zph[i], "\n")))
}
Iteration: 1 
NULL
Spec:  susinidum_oh *time 
NULL
AIC:  239724.019577566 
NULL
Cox zph test statistic:  4672.22758383257 
NULL
Iteration: 2 
NULL
Spec:  susinidum_oh *log(time+0.001) 
NULL
AIC:  241585.150871146 
NULL
Cox zph test statistic:  1204.10177494143 
NULL
Iteration: 3 
NULL
Spec:  susinidum_oh *poly(ifelse(is.na(time), .001,time), 3) 
NULL
AIC:  240219.630415016 
NULL
Cox zph test statistic:  7926.39955359493 
NULL
Iteration: 4 
NULL
Spec:  susinidum_oh *poly(ifelse(is.na(time), .001,time), 4) 
NULL
AIC:  240063.039259188 
NULL
Cox zph test statistic:  8212.21889149851 
NULL
Iteration: 5 
NULL
Spec:  susinidum_oh *poly(ifelse(is.na(time), .001,time), 5) 
NULL
AIC:  240016.095427946 
NULL
Cox zph test statistic:  8304.47329075385 
NULL
Iteration: 6 
NULL
Spec:  susinidum_oh *poly(ifelse(is.na(time), .001,time), 6) 
NULL
AIC:  239508.178249717 
NULL
Cox zph test statistic:  8101.01468172362 
NULL
Iteration: 7 
NULL
Spec:  susinidum_oh *pspline(log(time+0.001), df=3) 
NULL
AIC:  239508.178249717 
NULL
Cox zph test statistic:  8101.01468172362 
NULL
Iteration: 8 
NULL
Spec:  susinidum_oh *pspline(log(time+0.001), df=4) 
NULL
AIC:  239508.178249717 
NULL
Cox zph test statistic:  8101.01468172362 
NULL
Iteration: 9 
NULL
Spec:  susinidum_oh *pspline(log(time+0.001), df=5) 
NULL
AIC:  239508.178249717 
NULL
Cox zph test statistic:  8101.01468172362 
NULL
Iteration: 10 
NULL
Spec:  susinidum_oh *pspline(log(time+0.001), df=6) 
NULL
AIC:  239508.178249717 
NULL
Cox zph test statistic:  8101.01468172362 
NULL
Iteration: 11 
NULL
Spec:  susinidum_oh *bs(time, 3) 
NULL
AIC:  239518.026724213 
NULL
Cox zph test statistic:  10331.3482921498 
NULL
Iteration: 12 
NULL
Spec:  susinidum_oh *bs(time, 4) 
NULL
AIC:  236772.640457164 
NULL
Cox zph test statistic:  18133.928243881 
NULL
Iteration: 13 
NULL
Spec:  susinidum_oh *bs(time, 5) 
NULL
AIC:  235970.965928735 
NULL
Cox zph test statistic:  20900.8309746657 
NULL
Iteration: 14 
NULL
Spec:  susinidum_oh *bs(time, 6) 
NULL
AIC:  235677.262806318 
NULL
Cox zph test statistic:  21727.8304904265 
NULL
Iteration: 15 
NULL
Spec:  susinidum_oh *ns(time, 3) 
NULL
AIC:  238253.396508616 
NULL
Cox zph test statistic:  14728.7767838593 
NULL
Iteration: 16 
NULL
Spec:  susinidum_oh *ns(time, 4) 
NULL
AIC:  236653.226858842 
NULL
Cox zph test statistic:  19308.1808541226 
NULL
Iteration: 17 
NULL
Spec:  susinidum_oh *ns(time, 5) 
NULL
AIC:  236251.069626771 
NULL
Cox zph test statistic:  20993.031671471 
NULL
Iteration: 18 
NULL
Spec:  susinidum_oh *ns(time, 6) 
NULL
AIC:  235896.939555169 
NULL
Cox zph test statistic:  21101.9090902985 
NULL
Iteration: 19 
NULL
Spec:  susinidum_oh *rcs(time, 3) 
NULL
AIC:  239671.217452014 
NULL
Cox zph test statistic:  9313.41322424369 
NULL
X matrix deemed to be singular; variable time'' 
Iteration: 20 
NULL
Spec:  susinidum_oh *rcs(time, 4) 
NULL
AIC:  239671.217452014 
NULL
Cox zph test statistic:  9313.41322424369 
NULL
X matrix deemed to be singular; variable time'' time''' 
Iteration: 21 
NULL
Spec:  susinidum_oh *rcs(time, 5) 
NULL
AIC:  239671.217452014 
NULL
Cox zph test statistic:  9313.41322424369 
NULL
X matrix deemed to be singular; variable time'' time''' 
Iteration: 22 
NULL
Spec:  susinidum_oh *rcs(time, 6) 
NULL
AIC:  239671.217452014 
NULL
Cox zph test statistic:  9313.41322424369 
NULL
Code
results_susini_oh2[which(as.numeric(results_susini_oh2$cox_zph)==
                           min(as.numeric(results_susini_oh2$cox_zph))),]
                           spec      aic  cox_zph success
2 susinidum_oh *log(time+0.001) 241724.4 1088.605    TRUE
Code
#                            spec      aic cox_zph success
# 2 susinidum_oh *log(time+0.001) 241712.3 1088.12    TRUE
invisible("It worsen. We do nothing")

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
results_susinidum_oh2_alt <- data.frame(spec = character(), aic = numeric(), cox_zph = numeric(), success = logical())

for (i in seq(1,5,.1)) {
  change_points <- c(5*i, 10*i, 15*i)  # Example change points (months)
  coefficients <- c(0.5, 1.5, 0.5, 1)  # Example coefficients
  # Add the time-varying coefficient to your model
  
  data_mine_miss_restr_proc2$susinidum_oh2_rec2 <- with(data_mine_miss_restr_proc2,
                                                       susinidum_oh*time_varying_coeff(time, change_points, coefficients))

  model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_oh2_alt<-
    cph(as.formula(paste0("Surv(lag_time,time,event==1)~ 
            cluster(id)+ 
            lag_tr_outcome_rec +
            log_lag_dias_treat_imp_sin_na +
            lag_less_90d_tr1_rec+
            lag_comp_bpsc_y3_severe_rec + 
            lag_policonsumo2 + 
            edad_al_ing_1 + 
            ano_nac_corr + 
            susinidum_coc_rec2 +
            susinidum_oh2_rec2 +
            susinidum_pbc +
            susinidum_mar +
            psycom_dum_with +
            psycom_dum_study + 
            freq_cons_dum_5day +
            cond_oc_dum_2inact +
            cond_oc_dum_3unemp +
            susprindum_oh +
            susprindum_coc +
            susprindum_pbc +
            susprindum_mar+
            strat(tipo_de_plan_2_mod)")), 
        data=data_mine_miss_restr_proc2 %>% 
          data.table::as.data.table() %>% data.frame(), 
        x=TRUE, y=TRUE, surv=TRUE, iter.max = 250*4, tol = 1e-6)
  
  cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_oh2_alt)
  invisible("")
  print(paste0("iteration: ",i))
  aic_iter <- round(extractAIC(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_oh2_alt)[[2]],1)
  
  print( paste0("Time points: ",change_points[1],", ", change_points[2], ", ",change_points[3]))
  print( paste0("AIC: ", aic_iter))
  cox_zph_iter <- format(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_oh2_alt)$table[nrow(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_oh2_alt)$table), 1])
  print(paste0("Cox ZPH: ", cox_zph_iter))
  
  results_susinidum_oh2_alt <- rbind.data.frame(results_susinidum_oh2_alt, 
                                                 cbind.data.frame(
                                                   spec = paste0("iteration: ",i, "; change points: ", change_points[1],", ", change_points[2], ", ",change_points[3]), 
                                                   aic = aic_iter,
                                                   cox_zph = cox_zph_iter,
                                                   success = T
                                                 ))
}
[1] "iteration: 1"
[1] "Time points: 5, 10, 15"
[1] "AIC: 242713.4"
[1] "Cox ZPH: 346.5879"
[1] "iteration: 1.1"
[1] "Time points: 5.5, 11, 16.5"
[1] "AIC: 242719.6"
[1] "Cox ZPH: 326.4351"
[1] "iteration: 1.2"
[1] "Time points: 6, 12, 18"
[1] "AIC: 242716.7"
[1] "Cox ZPH: 322.6523"
[1] "iteration: 1.3"
[1] "Time points: 6.5, 13, 19.5"
[1] "AIC: 242727.5"
[1] "Cox ZPH: 323.4345"
[1] "iteration: 1.4"
[1] "Time points: 7, 14, 21"
[1] "AIC: 242733.8"
[1] "Cox ZPH: 337.7665"
[1] "iteration: 1.5"
[1] "Time points: 7.5, 15, 22.5"
[1] "AIC: 242738.1"
[1] "Cox ZPH: 364.8138"
[1] "iteration: 1.6"
[1] "Time points: 8, 16, 24"
[1] "AIC: 242739.8"
[1] "Cox ZPH: 379.663"
[1] "iteration: 1.7"
[1] "Time points: 8.5, 17, 25.5"
[1] "AIC: 242740.4"
[1] "Cox ZPH: 406.437"
[1] "iteration: 1.8"
[1] "Time points: 9, 18, 27"
[1] "AIC: 242739.5"
[1] "Cox ZPH: 419.1016"
[1] "iteration: 1.9"
[1] "Time points: 9.5, 19, 28.5"
[1] "AIC: 242737.2"
[1] "Cox ZPH: 434.1251"
[1] "iteration: 2"
[1] "Time points: 10, 20, 30"
[1] "AIC: 242733"
[1] "Cox ZPH: 442.814"
[1] "iteration: 2.1"
[1] "Time points: 10.5, 21, 31.5"
[1] "AIC: 242730"
[1] "Cox ZPH: 420.1959"
[1] "iteration: 2.2"
[1] "Time points: 11, 22, 33"
[1] "AIC: 242719.9"
[1] "Cox ZPH: 405.4619"
[1] "iteration: 2.3"
[1] "Time points: 11.5, 23, 34.5"
[1] "AIC: 242715.8"
[1] "Cox ZPH: 382.8406"
[1] "iteration: 2.4"
[1] "Time points: 12, 24, 36"
[1] "AIC: 242713.8"
[1] "Cox ZPH: 361.1335"
[1] "iteration: 2.5"
[1] "Time points: 12.5, 25, 37.5"
[1] "AIC: 242714.2"
[1] "Cox ZPH: 338.2163"
[1] "iteration: 2.6"
[1] "Time points: 13, 26, 39"
[1] "AIC: 242714.9"
[1] "Cox ZPH: 323.4996"
[1] "iteration: 2.7"
[1] "Time points: 13.5, 27, 40.5"
[1] "AIC: 242718.1"
[1] "Cox ZPH: 322.4792"
[1] "iteration: 2.8"
[1] "Time points: 14, 28, 42"
[1] "AIC: 242722.4"
[1] "Cox ZPH: 333.2702"
[1] "iteration: 2.9"
[1] "Time points: 14.5, 29, 43.5"
[1] "AIC: 242727"
[1] "Cox ZPH: 359.1076"
[1] "iteration: 3"
[1] "Time points: 15, 30, 45"
[1] "AIC: 242731.8"
[1] "Cox ZPH: 408.2805"
[1] "iteration: 3.1"
[1] "Time points: 15.5, 31, 46.5"
[1] "AIC: 242735.1"
[1] "Cox ZPH: 475.1458"
[1] "iteration: 3.2"
[1] "Time points: 16, 32, 48"
[1] "AIC: 242739"
[1] "Cox ZPH: 548.163"
[1] "iteration: 3.3"
[1] "Time points: 16.5, 33, 49.5"
[1] "AIC: 242740.2"
[1] "Cox ZPH: 628.4228"
[1] "iteration: 3.4"
[1] "Time points: 17, 34, 51"
[1] "AIC: 242740.2"
[1] "Cox ZPH: 715.4628"
[1] "iteration: 3.5"
[1] "Time points: 17.5, 35, 52.5"
[1] "AIC: 242738.7"
[1] "Cox ZPH: 801.7517"
[1] "iteration: 3.6"
[1] "Time points: 18, 36, 54"
[1] "AIC: 242734.7"
[1] "Cox ZPH: 899.7736"
[1] "iteration: 3.7"
[1] "Time points: 18.5, 37, 55.5"
[1] "AIC: 242731.9"
[1] "Cox ZPH: 982.7124"
[1] "iteration: 3.8"
[1] "Time points: 19, 38, 57"
[1] "AIC: 242723.8"
[1] "Cox ZPH: 1115.342"
[1] "iteration: 3.9"
[1] "Time points: 19.5, 39, 58.5"
[1] "AIC: 242713.1"
[1] "Cox ZPH: 1233.031"
[1] "iteration: 4"
[1] "Time points: 20, 40, 60"
[1] "AIC: 242698.8"
[1] "Cox ZPH: 1364.041"
[1] "iteration: 4.1"
[1] "Time points: 20.5, 41, 61.5"
[1] "AIC: 242688.1"
[1] "Cox ZPH: 1463.879"
[1] "iteration: 4.2"
[1] "Time points: 21, 42, 63"
[1] "AIC: 242672.2"
[1] "Cox ZPH: 1584.966"
[1] "iteration: 4.3"
[1] "Time points: 21.5, 43, 64.5"
[1] "AIC: 242659.5"
[1] "Cox ZPH: 1694.048"
[1] "iteration: 4.4"
[1] "Time points: 22, 44, 66"
[1] "AIC: 242631.3"
[1] "Cox ZPH: 1785.864"
[1] "iteration: 4.5"
[1] "Time points: 22.5, 45, 67.5"
[1] "AIC: 242619.1"
[1] "Cox ZPH: 1860.616"
[1] "iteration: 4.6"
[1] "Time points: 23, 46, 69"
[1] "AIC: 242597.9"
[1] "Cox ZPH: 1920.634"
[1] "iteration: 4.7"
[1] "Time points: 23.5, 47, 70.5"
[1] "AIC: 242583"
[1] "Cox ZPH: 2004.666"
[1] "iteration: 4.8"
[1] "Time points: 24, 48, 72"
[1] "AIC: 242566.1"
[1] "Cox ZPH: 2109.5"
[1] "iteration: 4.9"
[1] "Time points: 24.5, 49, 73.5"
[1] "AIC: 242550.8"
[1] "Cox ZPH: 2180.038"
[1] "iteration: 5"
[1] "Time points: 25, 50, 75"
[1] "AIC: 242534.7"
[1] "Cox ZPH: 2241.884"
Code
results_susinidum_oh2_alt[which(as.numeric(results_susinidum_oh2_alt$cox_zph)==
                                   min(as.numeric(results_susinidum_oh2_alt$cox_zph))),]
                                            spec      aic  cox_zph success
18 iteration: 2.7; change points: 13.5, 27, 40.5 242718.1 322.4792    TRUE
Code
#                                             spec      aic cox_zph success
# 18 iteration: 2.7; change points: 13.5, 27, 40.5 242706.4  322.13    TRUE

message(paste0("The minimum of the previous model: ",
               format(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc_alt)$table[nrow(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc_alt)$table), 1]),
               " vs. the minimum model: ",  results_susinidum_oh2_alt[which(as.numeric(results_susinidum_oh2_alt$cox_zph)== min(as.numeric(results_susinidum_oh2_alt$cox_zph))),"cox_zph"]
))

The minimum of the previous model: 317.7125 vs. the minimum model: 322.4792

Code
#The minimum of the first modified model: 321.3004 vs. the minimum model: 328.8011

invisible("It worsens. We do not integrate this model")

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
invisible("Analyse the violation of proportional hazards")
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

try(dev.off())
null device 
          1 
Code
z_240422_6_2_restr <- loess(res240422_6_restr[,"psycom_dum_with"] ~ time240422_6_restr, span=0.50)   # residuals 

par(mfrow= c(3,1))
plot(time240422_6_restr, fitted(z_240422_6_2_restr))
lines(supsmu(time240422_6_restr, res240422_6_restr[,"psycom_dum_with"]),lty=2)
title("Sch. res., psycom_dum_with")

plot(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc_alt)[which(attr(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc_alt$coefficients, "names") == "psycom_dum_with")], lwd=2, main= "Residuals of psycom_dum_with")
abline(0,0, col="red", lty=3, lwd=2)
abline(h=model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc_alt$coefficients["psycom_dum_with"], col=3, lwd=2, lty= 3)
legend("bottomleft", legend= c("Reference line for the null effect", "Average hazard over time", "Time-varying hazard"), lty=c(3,2,1), col= c("red",3,1), lwd=2, bty = "n", bg = "transparent")

km_psycom_dum_with <- survfit(Surv(lag_time,time,event)~ psycom_dum_with+ 
    cluster(id), data = data_mine_miss_restr_proc2)
plot(km_psycom_dum_with, fun = "cloglog", xlab = "Time (in days) using log",col=c("black", "red"), ylab = "log-log survival", main = "log-log curves by psycom_dum_with")
legend("bottomright", legend=c("=0", "=1"), col=c("black", "red"), lty=1, bty = "n", bg = "transparent")

recorded_plot <- recordPlot() 

folder_path <- ifelse(dir.exists("E:/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/"),
                      "E:/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/",
                      "C:/Users/CISS Fondecyt/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/")
png(paste0(folder_path,"zph/proportionallity_psycom_dum_with_restr.png"), height=13, width=10, res=500, units="in") 
recorded_plot
dev.off()
pdf 
  2 
Code
invisible("Now with Initial substance: Alcohol")

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
results_psycom_dum_with <- data.frame(spec = character(), aic = numeric(), cox_zph = numeric(), success = logical())

for (i in seq(.1,1.5,.025)) {
  tvcs <- c(20,80)*i
  
  data_mine_miss_restr_proc2$psycom_dum_with_rec<-
    create_time_varying_effect(data_mine_miss_restr_proc2$psycom_dum_with, 
                               data_mine_miss_restr_proc2$time, 
                               tvcs[1],
                               tvcs[2],
                               max(data_mine_miss_restr_proc2$time))

  model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc_alt_psycom_dum_with<-
    cph(as.formula(paste0("Surv(lag_time,time,event==1)~ 
            cluster(id)+ 
            lag_tr_outcome_rec +
            log_lag_dias_treat_imp_sin_na +
            lag_less_90d_tr1_rec+
            lag_comp_bpsc_y3_severe_rec + 
            lag_policonsumo2 + 
            edad_al_ing_1 + 
            ano_nac_corr + 
            susinidum_coc_rec2 +
            susinidum_oh +
            susinidum_pbc +
            susinidum_mar +
            psycom_dum_with_rec +
            psycom_dum_study + 
            freq_cons_dum_5day +
            cond_oc_dum_2inact +
            cond_oc_dum_3unemp +
            susprindum_oh +
            susprindum_coc +
            susprindum_pbc +
            susprindum_mar+
            strat(tipo_de_plan_2_mod)")), 
        data=data_mine_miss_restr_proc2 %>% 
          data.table::as.data.table() %>% data.frame(), 
        x=TRUE, y=TRUE, surv=TRUE, iter.max = 250*4, tol = 1e-6)
  
  cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc_alt_psycom_dum_with)
  invisible("")
  print(paste0("iteration: ",i))
  aic_iter <- round(extractAIC(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc_alt_psycom_dum_with)[[2]],1)
  print( paste0("Time points: ",tvcs[1],", ", tvcs[2]))
  print( paste0("AIC: ", aic_iter))
  cox_zph_iter <- format(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc_alt_psycom_dum_with)$table[nrow(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc_alt_psycom_dum_with)$table), 1])
  print(paste0("Cox ZPH: ", cox_zph_iter))
  
  results_psycom_dum_with <- rbind.data.frame(results_psycom_dum_with, 
                                             cbind.data.frame(
                                               spec = paste0("iteration: ",i, "; tvcs: ", tvcs[1],", ", tvcs[2]), 
                                               aic = aic_iter,
                                               cox_zph = cox_zph_iter,
                                               success = T
                                             ))
}
[1] "iteration: 0.1"
[1] "Time points: 2, 8"
[1] "AIC: 241851.2"
[1] "Cox ZPH: 1883.112"
[1] "iteration: 0.125"
[1] "Time points: 2.5, 10"
[1] "AIC: 241793.5"
[1] "Cox ZPH: 2047.944"
[1] "iteration: 0.15"
[1] "Time points: 3, 12"
[1] "AIC: 241741.1"
[1] "Cox ZPH: 2185.512"
[1] "iteration: 0.175"
[1] "Time points: 3.5, 14"
[1] "AIC: 241695.6"
[1] "Cox ZPH: 2289.825"
[1] "iteration: 0.2"
[1] "Time points: 4, 16"
[1] "AIC: 241659.1"
[1] "Cox ZPH: 2351.071"
[1] "iteration: 0.225"
[1] "Time points: 4.5, 18"
[1] "AIC: 241631.3"
[1] "Cox ZPH: 2370.834"
[1] "iteration: 0.25"
[1] "Time points: 5, 20"
[1] "AIC: 241612.3"
[1] "Cox ZPH: 2350.685"
[1] "iteration: 0.275"
[1] "Time points: 5.5, 22"
[1] "AIC: 241606.1"
[1] "Cox ZPH: 2272.468"
[1] "iteration: 0.3"
[1] "Time points: 6, 24"
[1] "AIC: 241612.6"
[1] "Cox ZPH: 2133.379"
[1] "iteration: 0.325"
[1] "Time points: 6.5, 26"
[1] "AIC: 241629.3"
[1] "Cox ZPH: 1950.946"
[1] "iteration: 0.35"
[1] "Time points: 7, 28"
[1] "AIC: 241658.1"
[1] "Cox ZPH: 1722"
[1] "iteration: 0.375"
[1] "Time points: 7.5, 30"
[1] "AIC: 241699.2"
[1] "Cox ZPH: 1448.429"
[1] "iteration: 0.4"
[1] "Time points: 8, 32"
[1] "AIC: 241749.9"
[1] "Cox ZPH: 1153.775"
[1] "iteration: 0.425"
[1] "Time points: 8.5, 34"
[1] "AIC: 241808.6"
[1] "Cox ZPH: 864.9711"
[1] "iteration: 0.45"
[1] "Time points: 9, 36"
[1] "AIC: 241873.7"
[1] "Cox ZPH: 612.0001"
[1] "iteration: 0.475"
[1] "Time points: 9.5, 38"
[1] "AIC: 241944.7"
[1] "Cox ZPH: 428.4835"
[1] "iteration: 0.5"
[1] "Time points: 10, 40"
[1] "AIC: 242017.1"
[1] "Cox ZPH: 355.5976"
[1] "iteration: 0.525"
[1] "Time points: 10.5, 42"
[1] "AIC: 242092.7"
[1] "Cox ZPH: 414.2425"
[1] "iteration: 0.55"
[1] "Time points: 11, 44"
[1] "AIC: 242169.9"
[1] "Cox ZPH: 617.0852"
[1] "iteration: 0.575"
[1] "Time points: 11.5, 46"
[1] "AIC: 242247.3"
[1] "Cox ZPH: 957.8698"
[1] "iteration: 0.6"
[1] "Time points: 12, 48"
[1] "AIC: 242323"
[1] "Cox ZPH: 1409.342"
[1] "iteration: 0.625"
[1] "Time points: 12.5, 50"
[1] "AIC: 242398.4"
[1] "Cox ZPH: 1950.015"
[1] "iteration: 0.65"
[1] "Time points: 13, 52"
[1] "AIC: 242471.1"
[1] "Cox ZPH: 2532.404"
[1] "iteration: 0.675"
[1] "Time points: 13.5, 54"
[1] "AIC: 242538.1"
[1] "Cox ZPH: 3100.883"
[1] "iteration: 0.7"
[1] "Time points: 14, 56"
[1] "AIC: 242599"
[1] "Cox ZPH: 3619.677"
[1] "iteration: 0.725"
[1] "Time points: 14.5, 58"
[1] "AIC: 242651.6"
[1] "Cox ZPH: 4060.109"
[1] "iteration: 0.75"
[1] "Time points: 15, 60"
[1] "AIC: 242694.7"
[1] "Cox ZPH: 4405.333"
[1] "iteration: 0.775"
[1] "Time points: 15.5, 62"
[1] "AIC: 242727.3"
[1] "Cox ZPH: 4647.372"
[1] "iteration: 0.8"
[1] "Time points: 16, 64"
[1] "AIC: 242747.8"
[1] "Cox ZPH: 4790.599"
[1] "iteration: 0.825"
[1] "Time points: 16.5, 66"
[1] "AIC: 242754.7"
[1] "Cox ZPH: 4840.749"
[1] "iteration: 0.85"
[1] "Time points: 17, 68"
[1] "AIC: 242746.9"
[1] "Cox ZPH: 4814.885"
[1] "iteration: 0.875"
[1] "Time points: 17.5, 70"
[1] "AIC: 242723.5"
[1] "Cox ZPH: 4725.884"
[1] "iteration: 0.9"
[1] "Time points: 18, 72"
[1] "AIC: 242683.9"
[1] "Cox ZPH: 4587.648"
[1] "iteration: 0.925"
[1] "Time points: 18.5, 74"
[1] "AIC: 242628.3"
[1] "Cox ZPH: 4412.073"
[1] "iteration: 0.95"
[1] "Time points: 19, 76"
[1] "AIC: 242556"
[1] "Cox ZPH: 4211.442"
[1] "iteration: 0.975"
[1] "Time points: 19.5, 78"
[1] "AIC: 242466.1"
[1] "Cox ZPH: 3995.497"
[1] "iteration: 1"
[1] "Time points: 20, 80"
[1] "AIC: 242357.4"
[1] "Cox ZPH: 3766.605"
[1] "iteration: 1.025"
[1] "Time points: 20.5, 82"
[1] "AIC: 242230.8"
[1] "Cox ZPH: 3532.535"
[1] "iteration: 1.05"
[1] "Time points: 21, 84"
[1] "AIC: 242086.1"
[1] "Cox ZPH: 3296.79"
[1] "iteration: 1.075"
[1] "Time points: 21.5, 86"
[1] "AIC: 241928.2"
[1] "Cox ZPH: 3068.773"
[1] "iteration: 1.1"
[1] "Time points: 22, 88"
[1] "AIC: 241757.4"
[1] "Cox ZPH: 2846.697"
[1] "iteration: 1.125"
[1] "Time points: 22.5, 90"
[1] "AIC: 241572.4"
[1] "Cox ZPH: 2629.15"
[1] "iteration: 1.15"
[1] "Time points: 23, 92"
[1] "AIC: 241374.5"
[1] "Cox ZPH: 2418.478"
[1] "iteration: 1.175"
[1] "Time points: 23.5, 94"
[1] "AIC: 241169.8"
[1] "Cox ZPH: 2218.64"
[1] "iteration: 1.2"
[1] "Time points: 24, 96"
[1] "AIC: 240956.5"
[1] "Cox ZPH: 2028.07"
[1] "iteration: 1.225"
[1] "Time points: 24.5, 98"
[1] "AIC: 240741.7"
[1] "Cox ZPH: 1853.869"
[1] "iteration: 1.25"
[1] "Time points: 25, 100"
[1] "AIC: 240521.7"
[1] "Cox ZPH: 1687.786"
[1] "iteration: 1.275"
[1] "Time points: 25.5, 102"
[1] "AIC: 240296.6"
[1] "Cox ZPH: 1534.548"
[1] "iteration: 1.3"
[1] "Time points: 26, 104"
[1] "AIC: 240084.3"
[1] "Cox ZPH: 1405.457"
[1] "iteration: 1.325"
[1] "Time points: 26.5, 106"
[1] "AIC: 239877.9"
[1] "Cox ZPH: 1295.962"
[1] "iteration: 1.35"
[1] "Time points: 27, 108"
[1] "AIC: 239675.8"
[1] "Cox ZPH: 1208.163"
[1] "iteration: 1.375"
[1] "Time points: 27.5, 110"
[1] "AIC: 239479.2"
[1] "Cox ZPH: 1148.109"
[1] "iteration: 1.4"
[1] "Time points: 28, 112"
[1] "AIC: 239290.5"
[1] "Cox ZPH: 1123.353"
[1] "iteration: 1.425"
[1] "Time points: 28.5, 114"
[1] "AIC: 239120.8"
[1] "Cox ZPH: 1142.212"
[1] "iteration: 1.45"
[1] "Time points: 29, 116"
[1] "AIC: 238980.7"
[1] "Cox ZPH: 1210.523"
[1] "iteration: 1.475"
[1] "Time points: 29.5, 118"
[1] "AIC: 238863.6"
[1] "Cox ZPH: 1343.285"
[1] "iteration: 1.5"
[1] "Time points: 30, 120"
[1] "AIC: 238771.2"
[1] "Cox ZPH: 1582.226"
Code
results_psycom_dum_with[which(as.numeric(results_psycom_dum_with$cox_zph)==
                               min(as.numeric(results_psycom_dum_with$cox_zph))),]
                           spec      aic  cox_zph success
17 iteration: 0.5; tvcs: 10, 40 242017.1 355.5976    TRUE
Code
#                            spec      aic  cox_zph success
# 17 iteration: 0.5; tvcs: 10, 40 242005.3 355.3837    TRUE

message(paste0("The previously modified model: ",
               round(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc_alt)$table[nrow(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc_alt)$table), 1],1),
               " vs. the minimum model: ",  results_psycom_dum_with[which(as.numeric(results_psycom_dum_with$cox_zph)== min(as.numeric(results_psycom_dum_with$cox_zph))),"cox_zph"]
))

The previously modified model: 317.7 vs. the minimum model: 355.5976

Code
#The previously modified model: 317.4 vs. the minimum model: 355.3837
invisible("It worsen")

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
require(splines)

specs_psycom_dum_with_rec <- paste0("psycom_dum_with *", c("time", "log(time+0.001)", "poly(ifelse(is.na(time), .001,time), 3)", "poly(ifelse(is.na(time), .001,time), 4)", "poly(ifelse(is.na(time), .001,time), 5)", "poly(ifelse(is.na(time), .001,time), 6)", "pspline(log(time+0.001), df=3)", "pspline(log(time+0.001), df=4)", "pspline(log(time+0.001), df=5)", "pspline(log(time+0.001), df=6)", "bs(time, 3)", "bs(time, 4)", "bs(time, 5)", "bs(time, 6)", "ns(time, 3)", "ns(time, 4)", "ns(time, 5)", "ns(time, 6)", "rcs(time, 3)", "rcs(time, 4)", "rcs(time, 5)", "rcs(time, 6)"))

results_psycom_dum_with_rec <- data.frame(spec = character(), aic = numeric(), cox_zph = numeric(), success = logical())

for (i in seq_along(specs_psycom_dum_with_rec)) {
  l <- specs_psycom_dum_with_rec[i]
   
  result <- tryCatch({
    model <- cph(as.formula(paste0("Surv(lag_time,time,event==1)~ 
            cluster(id)+ 
            lag_tr_outcome_rec +
            log_lag_dias_treat_imp_sin_na +
            lag_less_90d_tr1_rec+
            lag_comp_bpsc_y3_severe_rec + 
            lag_policonsumo2 + 
            edad_al_ing_1 + 
            ano_nac_corr + 
            susinidum_coc_rec2 +
            susinidum_oh +
            susinidum_pbc +
            susinidum_mar +
            I(round(",l,",2)) +
            psycom_dum_study + 
            freq_cons_dum_5day +
            cond_oc_dum_2inact +
            cond_oc_dum_3unemp +
            susprindum_oh +
            susprindum_coc +
            susprindum_pbc +
            susprindum_mar+
            strat(tipo_de_plan_2_mod)")), 
                 data=data_mine_miss_restr_proc2 %>% 
                   dplyr::mutate(log_lag_dias_treat_imp_sin_na_rec = bs(log_lag_dias_treat_imp_sin_na, 3)) %>% 
                   data.table::as.data.table() %>% data.frame(), 
                 x=TRUE, y=TRUE, surv=TRUE, iter.max = 250*4, tol = 1e-6)
    
    aic_val <- extractAIC(model)[[2]]
    cox_zph_val <- cox.zph(model)$table[nrow(cox.zph(model)$table), 1]
    success <- TRUE 
    
  }, error = function(e) {
    aic_val <- NA
    cox_zph_val <- NA
    success <- FALSE
    warning(paste0("Error fitting specification ", i, " (", l, "): ", e))
  })
  
  results_psycom_dum_with_rec <- rbind(results_psycom_dum_with_rec, data.frame(spec = l, aic = aic_val, cox_zph = cox_zph_val, success = success))
  
  print(cat(paste("Iteration:", i, "\n"))) # Add iteration number
  print(cat(paste("Spec: ", l, "\n")))
  print(cat(paste("AIC: ", results_psycom_dum_with_rec$aic[i], "\n"))) 
  print(cat(paste("Cox zph test statistic: ", results_psycom_dum_with_rec$cox_zph[i], "\n")))
}
Iteration: 1 
NULL
Spec:  psycom_dum_with *time 
NULL
AIC:  242073.646015772 
NULL
Cox zph test statistic:  1270.82638037256 
NULL
Iteration: 2 
NULL
Spec:  psycom_dum_with *log(time+0.001) 
NULL
AIC:  242679.202046089 
NULL
Cox zph test statistic:  495.282058112557 
NULL
Iteration: 3 
NULL
Spec:  psycom_dum_with *poly(ifelse(is.na(time), .001,time), 3) 
NULL
AIC:  241653.345412216 
NULL
Cox zph test statistic:  5532.18767068606 
NULL
Iteration: 4 
NULL
Spec:  psycom_dum_with *poly(ifelse(is.na(time), .001,time), 4) 
NULL
AIC:  241374.872334666 
NULL
Cox zph test statistic:  5845.6906904199 
NULL
Iteration: 5 
NULL
Spec:  psycom_dum_with *poly(ifelse(is.na(time), .001,time), 5) 
NULL
AIC:  241301.737606574 
NULL
Cox zph test statistic:  5895.95894768982 
NULL
Iteration: 6 
NULL
Spec:  psycom_dum_with *poly(ifelse(is.na(time), .001,time), 6) 
NULL
AIC:  241133.859850746 
NULL
Cox zph test statistic:  5814.08088848928 
NULL
Iteration: 7 
NULL
Spec:  psycom_dum_with *pspline(log(time+0.001), df=3) 
NULL
AIC:  241133.859850746 
NULL
Cox zph test statistic:  5814.08088848928 
NULL
Iteration: 8 
NULL
Spec:  psycom_dum_with *pspline(log(time+0.001), df=4) 
NULL
AIC:  241133.859850746 
NULL
Cox zph test statistic:  5814.08088848928 
NULL
Iteration: 9 
NULL
Spec:  psycom_dum_with *pspline(log(time+0.001), df=5) 
NULL
AIC:  241133.859850746 
NULL
Cox zph test statistic:  5814.08088848928 
NULL
Iteration: 10 
NULL
Spec:  psycom_dum_with *pspline(log(time+0.001), df=6) 
NULL
AIC:  241133.859850746 
NULL
Cox zph test statistic:  5814.08088848928 
NULL
Iteration: 11 
NULL
Spec:  psycom_dum_with *bs(time, 3) 
NULL
AIC:  240806.877886123 
NULL
Cox zph test statistic:  8350.2319648494 
NULL
Iteration: 12 
NULL
Spec:  psycom_dum_with *bs(time, 4) 
NULL
AIC:  237979.785769352 
NULL
Cox zph test statistic:  13977.8757470223 
NULL
Iteration: 13 
NULL
Spec:  psycom_dum_with *bs(time, 5) 
NULL
AIC:  237406.174681623 
NULL
Cox zph test statistic:  15528.301431604 
NULL
Iteration: 14 
NULL
Spec:  psycom_dum_with *bs(time, 6) 
NULL
AIC:  237229.627963973 
NULL
Cox zph test statistic:  16040.7359383214 
NULL
Iteration: 15 
NULL
Spec:  psycom_dum_with *ns(time, 3) 
NULL
AIC:  239286.89566843 
NULL
Cox zph test statistic:  11649.939360018 
NULL
Iteration: 16 
NULL
Spec:  psycom_dum_with *ns(time, 4) 
NULL
AIC:  237893.753000825 
NULL
Cox zph test statistic:  14575.1889880412 
NULL
Iteration: 17 
NULL
Spec:  psycom_dum_with *ns(time, 5) 
NULL
AIC:  237600.950385534 
NULL
Cox zph test statistic:  15639.9138673865 
NULL
Iteration: 18 
NULL
Spec:  psycom_dum_with *ns(time, 6) 
NULL
AIC:  237354.154406415 
NULL
Cox zph test statistic:  15735.8237332929 
NULL
Iteration: 19 
NULL
Spec:  psycom_dum_with *rcs(time, 3) 
NULL
AIC:  240998.015320773 
NULL
Cox zph test statistic:  7177.11023618446 
NULL
X matrix deemed to be singular; variable time'' 
Iteration: 20 
NULL
Spec:  psycom_dum_with *rcs(time, 4) 
NULL
AIC:  240998.015320773 
NULL
Cox zph test statistic:  7177.11023618446 
NULL
X matrix deemed to be singular; variable time'' time''' 
Iteration: 21 
NULL
Spec:  psycom_dum_with *rcs(time, 5) 
NULL
AIC:  240998.015320773 
NULL
Cox zph test statistic:  7177.11023618446 
NULL
X matrix deemed to be singular; variable time'' time''' 
Iteration: 22 
NULL
Spec:  psycom_dum_with *rcs(time, 6) 
NULL
AIC:  240998.015320773 
NULL
Cox zph test statistic:  7177.11023618446 
NULL
Code
results_psycom_dum_with_rec[which(as.numeric(results_psycom_dum_with_rec$cox_zph)==
                           min(as.numeric(results_psycom_dum_with_rec$cox_zph))),]
                              spec      aic  cox_zph success
2 psycom_dum_with *log(time+0.001) 242679.2 495.2821    TRUE
Code
#                               spec      aic  cox_zph success
# 2 psycom_dum_with *log(time+0.001) 242667.5 494.8698    TRUE
invisible("It worsen. We do nothing")

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
results_psycom_dum_with_alt <- data.frame(spec = character(), aic = numeric(), cox_zph = numeric(), success = logical())

for (i in seq(1,5,.1)) {
  change_points <- c(5*i, 10*i, 15*i)  # Example change points (months)
  coefficients <- c(0.5, 1.5, 0.5, 1)  # Example coefficients
  # Add the time-varying coefficient to your model
  
  data_mine_miss_restr_proc2$psycom_dum_with_rec2 <- with(data_mine_miss_restr_proc2,
                                                       psycom_dum_with*time_varying_coeff(time, change_points, coefficients))

  model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc_alt_psycom_dum_with_alt<-
    cph(as.formula(paste0("Surv(lag_time,time,event==1)~ 
            cluster(id)+ 
            lag_tr_outcome_rec +
            log_lag_dias_treat_imp_sin_na +
            lag_less_90d_tr1_rec+
            lag_comp_bpsc_y3_severe_rec + 
            lag_policonsumo2 + 
            edad_al_ing_1 + 
            ano_nac_corr + 
            susinidum_coc_rec2 +
            susinidum_oh +
            susinidum_pbc +
            susinidum_mar +
            psycom_dum_with_rec2 +
            psycom_dum_study + 
            freq_cons_dum_5day +
            cond_oc_dum_2inact +
            cond_oc_dum_3unemp +
            susprindum_oh +
            susprindum_coc +
            susprindum_pbc +
            susprindum_mar+
            strat(tipo_de_plan_2_mod)")), 
        data=data_mine_miss_restr_proc2 %>% 
          data.table::as.data.table() %>% data.frame(), 
        x=TRUE, y=TRUE, surv=TRUE, iter.max = 250*4, tol = 1e-6)
  
  cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc_alt_psycom_dum_with_alt)
  invisible("")
  print(paste0("iteration: ",i))
  aic_iter <- round(extractAIC(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc_alt_psycom_dum_with_alt)[[2]],1)
  
  print( paste0("Time points: ",change_points[1],", ", change_points[2], ", ",change_points[3]))
  print( paste0("AIC: ", aic_iter))
  cox_zph_iter <- format(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc_alt_psycom_dum_with_alt)$table[nrow(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc_alt_psycom_dum_with_alt)$table), 1])
  print(paste0("Cox ZPH: ", cox_zph_iter))
  
  results_psycom_dum_with_alt <- rbind.data.frame(results_psycom_dum_with_alt, 
                                                 cbind.data.frame(
                                                   spec = paste0("iteration: ",i, "; change points: ", change_points[1],", ", change_points[2], ", ",change_points[3]), 
                                                   aic = aic_iter,
                                                   cox_zph = cox_zph_iter,
                                                   success = T
                                                 ))
}
[1] "iteration: 1"
[1] "Time points: 5, 10, 15"
[1] "AIC: 242751.2"
[1] "Cox ZPH: 328.9806"
[1] "iteration: 1.1"
[1] "Time points: 5.5, 11, 16.5"
[1] "AIC: 242750.6"
[1] "Cox ZPH: 320.86"
[1] "iteration: 1.2"
[1] "Time points: 6, 12, 18"
[1] "AIC: 242750.2"
[1] "Cox ZPH: 315.1147"
[1] "iteration: 1.3"
[1] "Time points: 6.5, 13, 19.5"
[1] "AIC: 242747.3"
[1] "Cox ZPH: 308.1648"
[1] "iteration: 1.4"
[1] "Time points: 7, 14, 21"
[1] "AIC: 242746.4"
[1] "Cox ZPH: 306.6321"
[1] "iteration: 1.5"
[1] "Time points: 7.5, 15, 22.5"
[1] "AIC: 242745.3"
[1] "Cox ZPH: 306.8176"
[1] "iteration: 1.6"
[1] "Time points: 8, 16, 24"
[1] "AIC: 242742.7"
[1] "Cox ZPH: 307.9781"
[1] "iteration: 1.7"
[1] "Time points: 8.5, 17, 25.5"
[1] "AIC: 242739.7"
[1] "Cox ZPH: 310.7349"
[1] "iteration: 1.8"
[1] "Time points: 9, 18, 27"
[1] "AIC: 242740.6"
[1] "Cox ZPH: 311.398"
[1] "iteration: 1.9"
[1] "Time points: 9.5, 19, 28.5"
[1] "AIC: 242738.9"
[1] "Cox ZPH: 312.5175"
[1] "iteration: 2"
[1] "Time points: 10, 20, 30"
[1] "AIC: 242732.5"
[1] "Cox ZPH: 314.7717"
[1] "iteration: 2.1"
[1] "Time points: 10.5, 21, 31.5"
[1] "AIC: 242731.2"
[1] "Cox ZPH: 313.7358"
[1] "iteration: 2.2"
[1] "Time points: 11, 22, 33"
[1] "AIC: 242725.9"
[1] "Cox ZPH: 314.3701"
[1] "iteration: 2.3"
[1] "Time points: 11.5, 23, 34.5"
[1] "AIC: 242724.4"
[1] "Cox ZPH: 312.8812"
[1] "iteration: 2.4"
[1] "Time points: 12, 24, 36"
[1] "AIC: 242723"
[1] "Cox ZPH: 311.2805"
[1] "iteration: 2.5"
[1] "Time points: 12.5, 25, 37.5"
[1] "AIC: 242724.9"
[1] "Cox ZPH: 310.5611"
[1] "iteration: 2.6"
[1] "Time points: 13, 26, 39"
[1] "AIC: 242720.3"
[1] "Cox ZPH: 312.4336"
[1] "iteration: 2.7"
[1] "Time points: 13.5, 27, 40.5"
[1] "AIC: 242719.7"
[1] "Cox ZPH: 316.791"
[1] "iteration: 2.8"
[1] "Time points: 14, 28, 42"
[1] "AIC: 242719.8"
[1] "Cox ZPH: 325.1106"
[1] "iteration: 2.9"
[1] "Time points: 14.5, 29, 43.5"
[1] "AIC: 242724.8"
[1] "Cox ZPH: 336.6359"
[1] "iteration: 3"
[1] "Time points: 15, 30, 45"
[1] "AIC: 242727.3"
[1] "Cox ZPH: 351.6382"
[1] "iteration: 3.1"
[1] "Time points: 15.5, 31, 46.5"
[1] "AIC: 242730.9"
[1] "Cox ZPH: 372.395"
[1] "iteration: 3.2"
[1] "Time points: 16, 32, 48"
[1] "AIC: 242736.4"
[1] "Cox ZPH: 399.6994"
[1] "iteration: 3.3"
[1] "Time points: 16.5, 33, 49.5"
[1] "AIC: 242739.6"
[1] "Cox ZPH: 428.2439"
[1] "iteration: 3.4"
[1] "Time points: 17, 34, 51"
[1] "AIC: 242744.2"
[1] "Cox ZPH: 468.6018"
[1] "iteration: 3.5"
[1] "Time points: 17.5, 35, 52.5"
[1] "AIC: 242746.8"
[1] "Cox ZPH: 502.0081"
[1] "iteration: 3.6"
[1] "Time points: 18, 36, 54"
[1] "AIC: 242750"
[1] "Cox ZPH: 538.8976"
[1] "iteration: 3.7"
[1] "Time points: 18.5, 37, 55.5"
[1] "AIC: 242751.2"
[1] "Cox ZPH: 571.5965"
[1] "iteration: 3.8"
[1] "Time points: 19, 38, 57"
[1] "AIC: 242753.3"
[1] "Cox ZPH: 613.0588"
[1] "iteration: 3.9"
[1] "Time points: 19.5, 39, 58.5"
[1] "AIC: 242754.5"
[1] "Cox ZPH: 653.7076"
[1] "iteration: 4"
[1] "Time points: 20, 40, 60"
[1] "AIC: 242754.7"
[1] "Cox ZPH: 711.0753"
[1] "iteration: 4.1"
[1] "Time points: 20.5, 41, 61.5"
[1] "AIC: 242754.3"
[1] "Cox ZPH: 751.489"
[1] "iteration: 4.2"
[1] "Time points: 21, 42, 63"
[1] "AIC: 242752.7"
[1] "Cox ZPH: 801.2111"
[1] "iteration: 4.3"
[1] "Time points: 21.5, 43, 64.5"
[1] "AIC: 242750.3"
[1] "Cox ZPH: 852.915"
[1] "iteration: 4.4"
[1] "Time points: 22, 44, 66"
[1] "AIC: 242746.5"
[1] "Cox ZPH: 899.0964"
[1] "iteration: 4.5"
[1] "Time points: 22.5, 45, 67.5"
[1] "AIC: 242743.9"
[1] "Cox ZPH: 938.8631"
[1] "iteration: 4.6"
[1] "Time points: 23, 46, 69"
[1] "AIC: 242741.7"
[1] "Cox ZPH: 961.7692"
[1] "iteration: 4.7"
[1] "Time points: 23.5, 47, 70.5"
[1] "AIC: 242738.3"
[1] "Cox ZPH: 1000.855"
[1] "iteration: 4.8"
[1] "Time points: 24, 48, 72"
[1] "AIC: 242733.8"
[1] "Cox ZPH: 1045.778"
[1] "iteration: 4.9"
[1] "Time points: 24.5, 49, 73.5"
[1] "AIC: 242729.4"
[1] "Cox ZPH: 1075.742"
[1] "iteration: 5"
[1] "Time points: 25, 50, 75"
[1] "AIC: 242725.9"
[1] "Cox ZPH: 1113.625"
Code
results_psycom_dum_with_alt[which(as.numeric(results_psycom_dum_with_alt$cox_zph)==
                                   min(as.numeric(results_psycom_dum_with_alt$cox_zph))),]
                                      spec      aic  cox_zph success
5 iteration: 1.4; change points: 7, 14, 21 242746.4 306.6321    TRUE
Code
#                                       spec      aic  cox_zph success
# 5 iteration: 1.4; change points: 7, 14, 21 242734.7 306.2739    TRUE

message(paste0("The minimum of the previous model: ",
               format(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc_alt)$table[nrow(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc_alt)$table), 1]),
               " vs. the minimum model: ",  results_psycom_dum_with_alt[which(as.numeric(results_psycom_dum_with_alt$cox_zph)== min(as.numeric(results_psycom_dum_with_alt$cox_zph))),"cox_zph"]
))

The minimum of the previous model: 317.7125 vs. the minimum model: 306.6321

Code
#The minimum of the previous model: 317.3692 vs. the minimum model: 306.2739

invisible("It worsens. We do not integrate this model")

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
results_psycom_dum_with_alt[which(as.numeric(results_psycom_dum_with_alt$cox_zph)==
                                   min(as.numeric(results_psycom_dum_with_alt$cox_zph))),]
                                      spec      aic  cox_zph success
5 iteration: 1.4; change points: 7, 14, 21 242746.4 306.6321    TRUE
Code
change_points <- c(5, 10, 15)*1.4  # Example change points (months)
coefficients <- c(0.5, 1.5, 0.5, 1)  # Example coefficients
# Add the time-varying coefficient to your model

data_mine_miss_restr_proc2$psycom_dum_with_rec2 <- with(data_mine_miss_restr_proc2,
                                                     psycom_dum_with*time_varying_coeff(time, change_points, coefficients))

model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc_alt_psycom_dum_with_alt<-
  cph(Surv(lag_time,time,event==1)~ 
            cluster(id)+ 
            lag_tr_outcome_rec +
            log_lag_dias_treat_imp_sin_na +
            lag_less_90d_tr1_rec+
            lag_comp_bpsc_y3_severe_rec + 
            lag_policonsumo2 + 
            edad_al_ing_1 + 
            ano_nac_corr + 
            susinidum_coc_rec2 +
            susinidum_oh +
            susinidum_pbc +
            susinidum_mar +
            psycom_dum_with_rec2 +
            psycom_dum_study + 
            freq_cons_dum_5day +
            cond_oc_dum_2inact +
            cond_oc_dum_3unemp +
            susprindum_oh +
            susprindum_coc +
            susprindum_pbc +
            susprindum_mar+
            strat(tipo_de_plan_2_mod), 
      data=data_mine_miss_restr_proc2 %>% 
        data.table::as.data.table() %>% data.frame(), 
      x=TRUE, y=TRUE, surv=TRUE, iter.max = 250*4, tol = 1e-6)

round(cox.zph(model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc_alt_psycom_dum_with_alt)$table,4)
                                 chisq df      p
lag_tr_outcome_rec             66.0738  1 0.0000
log_lag_dias_treat_imp_sin_na  10.3740  1 0.0013
lag_less_90d_tr1_rec           83.7240  1 0.0000
lag_comp_bpsc_y3_severe_rec    17.2145  1 0.0000
lag_policonsumo2                3.2530  1 0.0713
edad_al_ing_1                   0.1557  1 0.6931
ano_nac_corr                    5.1059  1 0.0238
susinidum_coc_rec2              2.9522  1 0.0858
susinidum_oh                   19.8355  1 0.0000
susinidum_pbc                   2.0732  1 0.1499
susinidum_mar                   6.7703  1 0.0093
psycom_dum_with_rec2            0.7582  1 0.3839
psycom_dum_study                1.4761  1 0.2244
freq_cons_dum_5day              0.0802  1 0.7770
cond_oc_dum_2inact              4.4838  1 0.0342
cond_oc_dum_3unemp              1.7238  1 0.1892
susprindum_oh                   4.4440  1 0.0350
susprindum_coc                  1.5636  1 0.2111
susprindum_pbc                  0.0418  1 0.8380
susprindum_mar                  5.2227  1 0.0223
GLOBAL                        306.6321 20 0.0000
Code
#                                  chisq df      p
# lag_tr_outcome_rec             66.1801  1 0.0000 ####
# log_lag_dias_treat_imp_sin_na  10.2171  1 0.0014 #
# lag_less_90d_tr1_rec           83.7258  1 0.0000 ####
# lag_comp_bpsc_y3_severe_rec    17.1686  1 0.0000 ####
# lag_policonsumo2                3.2220  1 0.0727
# edad_al_ing_1                   0.1476  1 0.7008
# ano_nac_corr                    5.1430  1 0.0233
# susinidum_coc_rec2              2.9413  1 0.0863
# susinidum_oh                   19.6325  1 0.0000 ####
# susinidum_pbc                   1.9940  1 0.1579
# susinidum_mar                   6.7367  1 0.0094 #
# psycom_dum_with_rec2            0.7771  1 0.3780
# psycom_dum_study                1.4873  1 0.2226
# freq_cons_dum_5day              0.0522  1 0.8193
# cond_oc_dum_2inact              4.5082  1 0.0337
# cond_oc_dum_3unemp              1.6053  1 0.2051
# susprindum_oh                   4.3562  1 0.0369
# susprindum_coc                  1.6009  1 0.2058
# susprindum_pbc                  0.0568  1 0.8117
# susprindum_mar                  5.2723  1 0.0217
# GLOBAL                        306.2739 20 0.0000

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
invisible("Analyse the violation of proportional hazards")
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
Apply
Code
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
invisible("IIW")
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:

levels_tipo_de_plan_2_mod <-attr(table(data_mine_miss_restr_proc2$tipo_de_plan_2_mod),"name")

invisible("model_restr_after_mod_not_log_lag_dias_treat_lag_less_90d_bpsc_severe_alt_tr_out_susinidum_coc_alt_psycom_dum_with_alt, make IIWs")

invisible("basic ambulatory")

iiw_model_after_ph_ba<-
iiw.weights(Surv(time.lag,time,event)~ 
            cluster(id)+ 
            tr_outcome_rec.lag +
            log_dias_treat_imp_sin_na.lag +
            less_90d_tr1_rec.lag+
            comp_bpsc_y3_severe_rec.lag + 
            policonsumo2.lag + 
            edad_al_ing_1 + 
            ano_nac_corr + 
            susinidum_coc_rec2 +
            susinidum_oh +
            susinidum_pbc +
            susinidum_mar +
            psycom_dum_with_rec2 +
            psycom_dum_study + 
            freq_cons_dum_5day +
            cond_oc_dum_2inact +
            cond_oc_dum_3unemp +
            susprindum_oh +
            susprindum_coc +
            susprindum_pbc +
            susprindum_mar, 
            data= subset(data_mine_miss_restr_proc2, tipo_de_plan_2_mod== levels_tipo_de_plan_2_mod[[1]]) %>% as.data.frame(),
            id= "id",
            time= "time",
            event= "event", #character string indicating which column of the data indicates whether or not a visit occurred. If every row corresponds to a visit, then this column will consist entirely of ones
            maxfu= subset(maxfu_restr_df, subset=  maxfu.id %in% subset(data_mine_miss_restr_proc2, tipo_de_plan_2_mod==levels_tipo_de_plan_2_mod[[1]])$id) %>% as.data.frame(),
            # formulanull = Surv(time.lag,time,event)~ edad_al_ing_1 + 
            #       ano_nac_corr,
            invariant= c("edad_al_ing_1", "ano_nac_corr", "susinidum_coc_rec2", "susinidum_oh", "susinidum_pbc", "susinidum_mar",  "psycom_dum_with_rec2", "psycom_dum_study", "freq_cons_dum_5day", "cond_oc_dum_2inact", "cond_oc_dum_3unemp", "susprindum_oh", "susprindum_coc", "susprindum_pbc", "susprindum_mar"),
            lagvars= c("time", "log_dias_treat_imp_sin_na","tr_outcome_rec", "comp_bpsc_y3_severe_rec", "less_90d_tr1_rec", "policonsumo2"),
            lagfirst= c(2.95082,4.499811/2,0,0,0,0),  #90/30.5 4.499811 es 90 días
            first= T
)

invisible("2024-05-02: problem with different values in people with plans (possibly imputed)")

dis_run_w_more_type_plan<-
data_mine_miss_restr_proc2 %>% 
    dplyr::group_by(id) %>% 
    dplyr::summarise(count_n_dis=n_distinct(tipo_de_plan_2_mod)) %>% 
    dplyr::filter(count_n_dis>1) %>% distinct(id)


summary(iiw_model_after_ph_ba$m)
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome_rec.lag + 
    log_dias_treat_imp_sin_na.lag + less_90d_tr1_rec.lag + comp_bpsc_y3_severe_rec.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_coc_rec2 + 
    susinidum_oh + susinidum_pbc + susinidum_mar + psycom_dum_with_rec2 + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 13471, number of events= 9113 
   (880 observations deleted due to missingness)

                                   coef exp(coef)  se(coef) robust se       z
tr_outcome_rec.lag             0.170110  1.185436  0.055701  0.082986   2.050
log_dias_treat_imp_sin_na.lag -0.236237  0.789594  0.006277  0.022273 -10.606
less_90d_tr1_rec.lag          -0.112838  0.893295  0.070938  0.086272  -1.308
comp_bpsc_y3_severe_rec.lag    0.034013  1.034598  0.092579  0.074490   0.457
policonsumo2.lag              -0.636014  0.529398  0.025642  0.026439 -24.056
edad_al_ing_1                  0.039205  1.039984  0.005187  0.003358  11.675
ano_nac_corr                   0.044017  1.045000  0.005112  0.003333  13.207
susinidum_coc_rec2             0.166251  1.180869  0.082712  0.064699   2.570
susinidum_oh                   0.102885  1.108363  0.064329  0.049501   2.078
susinidum_pbc                 -0.016869  0.983272  0.076770  0.057891  -0.291
susinidum_mar                  0.097263  1.102151  0.065595  0.052648   1.847
psycom_dum_with_rec2           0.015614  1.015737  0.021836  0.014045   1.112
psycom_dum_study              -0.182551  0.833142  0.032157  0.030477  -5.990
freq_cons_dum_5day            -0.016709  0.983430  0.024123  0.020375  -0.820
cond_oc_dum_2inact            -0.002615  0.997388  0.033110  0.025535  -0.102
cond_oc_dum_3unemp             0.066169  1.068408  0.025464  0.017982   3.680
susprindum_oh                 -0.019388  0.980799  0.097556  0.059609  -0.325
susprindum_coc                 0.101071  1.106356  0.098804  0.060316   1.676
susprindum_pbc                 0.118128  1.125388  0.097381  0.060901   1.940
susprindum_mar                 0.122394  1.130199  0.103347  0.062593   1.955
                              Pr(>|z|)    
tr_outcome_rec.lag            0.040377 *  
log_dias_treat_imp_sin_na.lag  < 2e-16 ***
less_90d_tr1_rec.lag          0.190897    
comp_bpsc_y3_severe_rec.lag   0.647949    
policonsumo2.lag               < 2e-16 ***
edad_al_ing_1                  < 2e-16 ***
ano_nac_corr                   < 2e-16 ***
susinidum_coc_rec2            0.010181 *  
susinidum_oh                  0.037671 *  
susinidum_pbc                 0.770749    
susinidum_mar                 0.064684 .  
psycom_dum_with_rec2          0.266259    
psycom_dum_study               2.1e-09 ***
freq_cons_dum_5day            0.412174    
cond_oc_dum_2inact            0.918429    
cond_oc_dum_3unemp            0.000233 ***
susprindum_oh                 0.744993    
susprindum_coc                0.093799 .  
susprindum_pbc                0.052420 .  
susprindum_mar                0.050537 .  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome_rec.lag               1.1854     0.8436    1.0075    1.3948
log_dias_treat_imp_sin_na.lag    0.7896     1.2665    0.7559    0.8248
less_90d_tr1_rec.lag             0.8933     1.1195    0.7543    1.0579
comp_bpsc_y3_severe_rec.lag      1.0346     0.9666    0.8941    1.1972
policonsumo2.lag                 0.5294     1.8889    0.5027    0.5576
edad_al_ing_1                    1.0400     0.9616    1.0332    1.0469
ano_nac_corr                     1.0450     0.9569    1.0382    1.0518
susinidum_coc_rec2               1.1809     0.8468    1.0402    1.3405
susinidum_oh                     1.1084     0.9022    1.0059    1.2213
susinidum_pbc                    0.9833     1.0170    0.8778    1.1014
susinidum_mar                    1.1022     0.9073    0.9941    1.2220
psycom_dum_with_rec2             1.0157     0.9845    0.9882    1.0441
psycom_dum_study                 0.8331     1.2003    0.7848    0.8844
freq_cons_dum_5day               0.9834     1.0168    0.9449    1.0235
cond_oc_dum_2inact               0.9974     1.0026    0.9487    1.0486
cond_oc_dum_3unemp               1.0684     0.9360    1.0314    1.1067
susprindum_oh                    0.9808     1.0196    0.8727    1.1023
susprindum_coc                   1.1064     0.9039    0.9830    1.2452
susprindum_pbc                   1.1254     0.8886    0.9988    1.2681
susprindum_mar                   1.1302     0.8848    0.9997    1.2777

Concordance= 0.635  (se = 0.002 )
Likelihood ratio test= 2204  on 20 df,   p=<2e-16
Wald test            = 1947  on 20 df,   p=<2e-16
Score (logrank) test = 2554  on 20 df,   p=<2e-16,   Robust = 2679  p=<2e-16

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
Code
# Call:
# coxph(formula = Surv(time.lag, time, event) ~ tr_outcome_rec.lag + 
#     log_dias_treat_imp_sin_na.lag + less_90d_tr1_rec.lag + comp_bpsc_y3_severe_rec.lag + 
#     policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_coc_rec2 + 
#     susinidum_oh + susinidum_pbc + susinidum_mar + psycom_dum_with_rec2 + 
#     psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
#     cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
#     susprindum_mar, data = datacox, cluster = id)
# 
#   n= 13471, number of events= 9113 
#    (880 observations deleted due to missingness)
# 
#                                    coef exp(coef)  se(coef) robust se       z Pr(>|z|)    
# tr_outcome_rec.lag            -0.053168  0.948220  0.057677  0.039772  -1.337  0.18128    
# log_dias_treat_imp_sin_na.lag -0.038928  0.961820  0.013773  0.015230  -2.556  0.01059 *  
# less_90d_tr1_rec.lag           0.095160  1.099835  0.073449  0.053940   1.764  0.07770 .  
# comp_bpsc_y3_severe_rec.lag    0.009360  1.009404  0.092595  0.069811   0.134  0.89335    
# policonsumo2.lag              -0.799276  0.449654  0.024645  0.019765 -40.438  < 2e-16 ***
# edad_al_ing_1                  0.023181  1.023452  0.005167  0.003189   7.268 3.64e-13 ***
# ano_nac_corr                   0.030272  1.030735  0.005099  0.003160   9.581  < 2e-16 ***
# susinidum_coc_rec2             0.113215  1.119873  0.081735  0.057953   1.954  0.05075 .  
# susinidum_oh                   0.064995  1.067154  0.064022  0.041330   1.573  0.11581    
# susinidum_pbc                 -0.091810  0.912278  0.076500  0.051320  -1.789  0.07362 .  
# susinidum_mar                  0.070750  1.073313  0.065353  0.042346   1.671  0.09477 .  
# psycom_dum_with_rec2           0.009316  1.009359  0.021927  0.012820   0.727  0.46744    
# psycom_dum_study              -0.172542  0.841523  0.032163  0.024628  -7.006 2.45e-12 ***
# freq_cons_dum_5day             0.002757  1.002760  0.024133  0.014661   0.188  0.85086    
# cond_oc_dum_2inact            -0.019184  0.980999  0.033110  0.020755  -0.924  0.35532    
# cond_oc_dum_3unemp             0.053533  1.054991  0.025441  0.016401   3.264  0.00110 ** 
# susprindum_oh                  0.004280  1.004289  0.097915  0.059290   0.072  0.94245    
# susprindum_coc                 0.167440  1.182274  0.099029  0.060961   2.747  0.00602 ** 
# susprindum_pbc                 0.166351  1.180988  0.097732  0.060049   2.770  0.00560 ** 
# susprindum_mar                 0.152467  1.164705  0.103688  0.062710   2.431  0.01504 *  
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# 
#                               exp(coef) exp(-coef) lower .95 upper .95
# tr_outcome_rec.lag               0.9482     1.0546    0.8771    1.0251
# log_dias_treat_imp_sin_na.lag    0.9618     1.0397    0.9335    0.9910
# less_90d_tr1_rec.lag             1.0998     0.9092    0.9895    1.2225
# comp_bpsc_y3_severe_rec.lag      1.0094     0.9907    0.8803    1.1574
# policonsumo2.lag                 0.4497     2.2239    0.4326    0.4674
# edad_al_ing_1                    1.0235     0.9771    1.0171    1.0299
# ano_nac_corr                     1.0307     0.9702    1.0244    1.0371
# susinidum_coc_rec2               1.1199     0.8930    0.9996    1.2546
# susinidum_oh                     1.0672     0.9371    0.9841    1.1572
# susinidum_pbc                    0.9123     1.0962    0.8250    1.0088
# susinidum_mar                    1.0733     0.9317    0.9878    1.1662
# psycom_dum_with_rec2             1.0094     0.9907    0.9843    1.0350
# psycom_dum_study                 0.8415     1.1883    0.8019    0.8831
# freq_cons_dum_5day               1.0028     0.9972    0.9744    1.0320
# cond_oc_dum_2inact               0.9810     1.0194    0.9419    1.0217
# cond_oc_dum_3unemp               1.0550     0.9479    1.0216    1.0895
# susprindum_oh                    1.0043     0.9957    0.8941    1.1280
# susprindum_coc                   1.1823     0.8458    1.0491    1.3323
# susprindum_pbc                   1.1810     0.8467    1.0499    1.3285
# susprindum_mar                   1.1647     0.8586    1.0300    1.3170
# 
# Concordance= 0.609  (se = 0.002 )
# Likelihood ratio test= 1284  on 20 df,   p=<2e-16
# Wald test            = 2579  on 20 df,   p=<2e-16
# Score (logrank) test = 1333  on 20 df,   p=<2e-16,   Robust = 2025  p=<2e-16
# 
#   (Note: the likelihood ratio and score tests assume independence of
#      observations within a cluster, the Wald and robust score tests do not).

summary(iiw_model_after_ph_ba$iiw)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
0.09172 0.55656 1.00031 1.24301 1.82741 5.01220 
Code
 #   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 # 0.5685  0.8381  1.0387  1.3327  1.8348  3.4639 

iiw_model_after_ph_ba_alt<-
iiw.weights(Surv(time.lag,time,event)~ 
            cluster(id)+ 
            tr_outcome_rec.lag +
            log_dias_treat_imp_sin_na.lag +
            less_90d_tr1_rec.lag+
            comp_bpsc_y3_severe_rec.lag + 
            policonsumo2.lag + 
            edad_al_ing_1 + 
            ano_nac_corr + 
            susinidum_coc_rec2 +
            susinidum_oh +
            susinidum_pbc +
            susinidum_mar +
            psycom_dum_with_rec2 +
            psycom_dum_study + 
            freq_cons_dum_5day +
            cond_oc_dum_2inact +
            cond_oc_dum_3unemp +
            susprindum_oh +
            susprindum_coc +
            susprindum_pbc +
            susprindum_mar, 
            data= subset(data_mine_miss_restr_proc2, tipo_de_plan_2_mod== levels_tipo_de_plan_2_mod[[1]]) %>% as.data.frame(),
            id= "id",
            time= "time",
            event= "event", #character string indicating which column of the data indicates whether or not a visit occurred. If every row corresponds to a visit, then this column will consist entirely of ones
            maxfu= subset(maxfu_restr_df, subset=  maxfu.id %in% subset(data_mine_miss_restr_proc2, tipo_de_plan_2_mod==levels_tipo_de_plan_2_mod[[1]])$id) %>% as.data.frame(),
            invariant= c("edad_al_ing_1", "ano_nac_corr", "susinidum_coc_rec2", "susinidum_oh", "susinidum_pbc", "susinidum_mar",  "psycom_dum_with_rec2", "psycom_dum_study", "freq_cons_dum_5day", "cond_oc_dum_2inact", "cond_oc_dum_3unemp", "susprindum_oh", "susprindum_coc", "susprindum_pbc", "susprindum_mar"),
            lagvars= c("time", "log_dias_treat_imp_sin_na","tr_outcome_rec", "comp_bpsc_y3_severe_rec", "less_90d_tr1_rec", "policonsumo2"),
            lagfirst= c(2.95082,4.499811,1,1,1,1),  #90/30.5 4.499811 es 90 días
            first= T
)
summary(iiw_model_after_ph_ba_alt$m)
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome_rec.lag + 
    log_dias_treat_imp_sin_na.lag + less_90d_tr1_rec.lag + comp_bpsc_y3_severe_rec.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_coc_rec2 + 
    susinidum_oh + susinidum_pbc + susinidum_mar + psycom_dum_with_rec2 + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 13471, number of events= 9113 
   (880 observations deleted due to missingness)

                                   coef exp(coef)  se(coef) robust se      z
tr_outcome_rec.lag             2.114216  8.283090  0.072640  0.068485 30.871
log_dias_treat_imp_sin_na.lag  0.484348  1.623117  0.023209  0.028675 16.891
less_90d_tr1_rec.lag           0.399869  1.491630  0.070287  0.062216  6.427
comp_bpsc_y3_severe_rec.lag    0.692117  1.997940  0.055532  0.054833 12.622
policonsumo2.lag              -0.009252  0.990791  0.032970  0.027511 -0.336
edad_al_ing_1                  0.095935  1.100687  0.005243  0.004801 19.983
ano_nac_corr                   0.101247  1.106550  0.005183  0.004728 21.415
susinidum_coc_rec2             0.162364  1.176289  0.080265  0.090627  1.792
susinidum_oh                   0.049976  1.051246  0.063197  0.057214  0.873
susinidum_pbc                  0.067771  1.070120  0.075944  0.068309  0.992
susinidum_mar                  0.080871  1.084231  0.064602  0.058516  1.382
psycom_dum_with_rec2          -0.077395  0.925524  0.021792  0.017770 -4.355
psycom_dum_study              -0.171662  0.842263  0.032046  0.030499 -5.629
freq_cons_dum_5day            -0.013027  0.987058  0.024045  0.021068 -0.618
cond_oc_dum_2inact            -0.005455  0.994559  0.033066  0.028766 -0.190
cond_oc_dum_3unemp             0.047916  1.049082  0.025405  0.022455  2.134
susprindum_oh                  0.124032  1.132052  0.097439  0.078021  1.590
susprindum_coc                 0.176572  1.193120  0.098636  0.079742  2.214
susprindum_pbc                 0.217877  1.243434  0.097414  0.078704  2.768
susprindum_mar                 0.183830  1.201811  0.103376  0.083412  2.204
                              Pr(>|z|)    
tr_outcome_rec.lag             < 2e-16 ***
log_dias_treat_imp_sin_na.lag  < 2e-16 ***
less_90d_tr1_rec.lag          1.30e-10 ***
comp_bpsc_y3_severe_rec.lag    < 2e-16 ***
policonsumo2.lag               0.73665    
edad_al_ing_1                  < 2e-16 ***
ano_nac_corr                   < 2e-16 ***
susinidum_coc_rec2             0.07320 .  
susinidum_oh                   0.38240    
susinidum_pbc                  0.32114    
susinidum_mar                  0.16696    
psycom_dum_with_rec2          1.33e-05 ***
psycom_dum_study              1.82e-08 ***
freq_cons_dum_5day             0.53636    
cond_oc_dum_2inact             0.84959    
cond_oc_dum_3unemp             0.03285 *  
susprindum_oh                  0.11190    
susprindum_coc                 0.02681 *  
susprindum_pbc                 0.00563 ** 
susprindum_mar                 0.02753 *  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome_rec.lag               8.2831     0.1207    7.2427    9.4730
log_dias_treat_imp_sin_na.lag    1.6231     0.6161    1.5344    1.7170
less_90d_tr1_rec.lag             1.4916     0.6704    1.3204    1.6851
comp_bpsc_y3_severe_rec.lag      1.9979     0.5005    1.7944    2.2246
policonsumo2.lag                 0.9908     1.0093    0.9388    1.0457
edad_al_ing_1                    1.1007     0.9085    1.0904    1.1111
ano_nac_corr                     1.1065     0.9037    1.0963    1.1169
susinidum_coc_rec2               1.1763     0.8501    0.9849    1.4049
susinidum_oh                     1.0512     0.9513    0.9397    1.1760
susinidum_pbc                    1.0701     0.9345    0.9360    1.2234
susinidum_mar                    1.0842     0.9223    0.9667    1.2160
psycom_dum_with_rec2             0.9255     1.0805    0.8938    0.9583
psycom_dum_study                 0.8423     1.1873    0.7934    0.8941
freq_cons_dum_5day               0.9871     1.0131    0.9471    1.0287
cond_oc_dum_2inact               0.9946     1.0055    0.9400    1.0522
cond_oc_dum_3unemp               1.0491     0.9532    1.0039    1.0963
susprindum_oh                    1.1321     0.8834    0.9715    1.3191
susprindum_coc                   1.1931     0.8381    1.0205    1.3950
susprindum_pbc                   1.2434     0.8042    1.0657    1.4508
susprindum_mar                   1.2018     0.8321    1.0206    1.4153

Concordance= 0.706  (se = 0.002 )
Likelihood ratio test= 5371  on 20 df,   p=<2e-16
Wald test            = 5053  on 20 df,   p=<2e-16
Score (logrank) test = 5632  on 20 df,   p=<2e-16,   Robust = 3864  p=<2e-16

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
Code
# Call:
# coxph(formula = Surv(time.lag, time, event) ~ tr_outcome_rec.lag + 
#     log_dias_treat_imp_sin_na.lag + less_90d_tr1_rec.lag + comp_bpsc_y3_severe_rec.lag + 
#     policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_coc_rec2 + 
#     susinidum_oh + susinidum_pbc + susinidum_mar + psycom_dum_with_rec2 + 
#     psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
#     cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
#     susprindum_mar, data = datacox, cluster = id)
# 
#   n= 13471, number of events= 9113 
#    (880 observations deleted due to missingness)
# 
#                                    coef exp(coef)  se(coef) robust se      z Pr(>|z|)    
# tr_outcome_rec.lag             2.114216  8.283090  0.072640  0.068485 30.871  < 2e-16 ***
# log_dias_treat_imp_sin_na.lag  0.484348  1.623117  0.023209  0.028675 16.891  < 2e-16 ***
# less_90d_tr1_rec.lag           0.399869  1.491630  0.070287  0.062216  6.427 1.30e-10 ***
# comp_bpsc_y3_severe_rec.lag    0.692117  1.997940  0.055532  0.054833 12.622  < 2e-16 ***
# policonsumo2.lag              -0.009252  0.990791  0.032970  0.027511 -0.336  0.73665    
# edad_al_ing_1                  0.095935  1.100687  0.005243  0.004801 19.983  < 2e-16 ***
# ano_nac_corr                   0.101247  1.106550  0.005183  0.004728 21.415  < 2e-16 ***
# susinidum_coc_rec2             0.162364  1.176289  0.080265  0.090627  1.792  0.07320 .  
# susinidum_oh                   0.049976  1.051246  0.063197  0.057214  0.873  0.38240    
# susinidum_pbc                  0.067771  1.070120  0.075944  0.068309  0.992  0.32114    
# susinidum_mar                  0.080871  1.084231  0.064602  0.058516  1.382  0.16696    
# psycom_dum_with_rec2          -0.077395  0.925524  0.021792  0.017770 -4.355 1.33e-05 ***
# psycom_dum_study              -0.171662  0.842263  0.032046  0.030499 -5.629 1.82e-08 ***
# freq_cons_dum_5day            -0.013027  0.987058  0.024045  0.021068 -0.618  0.53636    
# cond_oc_dum_2inact            -0.005455  0.994559  0.033066  0.028766 -0.190  0.84959    
# cond_oc_dum_3unemp             0.047916  1.049082  0.025405  0.022455  2.134  0.03285 *  
# susprindum_oh                  0.124032  1.132052  0.097439  0.078021  1.590  0.11190    
# susprindum_coc                 0.176572  1.193120  0.098636  0.079742  2.214  0.02681 *  
# susprindum_pbc                 0.217877  1.243434  0.097414  0.078704  2.768  0.00563 ** 
# susprindum_mar                 0.183830  1.201811  0.103376  0.083412  2.204  0.02753 *  
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# 
#                               exp(coef) exp(-coef) lower .95 upper .95
# tr_outcome_rec.lag               8.2831     0.1207    7.2427    9.4730
# log_dias_treat_imp_sin_na.lag    1.6231     0.6161    1.5344    1.7170
# less_90d_tr1_rec.lag             1.4916     0.6704    1.3204    1.6851
# comp_bpsc_y3_severe_rec.lag      1.9979     0.5005    1.7944    2.2246
# policonsumo2.lag                 0.9908     1.0093    0.9388    1.0457
# edad_al_ing_1                    1.1007     0.9085    1.0904    1.1111
# ano_nac_corr                     1.1065     0.9037    1.0963    1.1169
# susinidum_coc_rec2               1.1763     0.8501    0.9849    1.4049
# susinidum_oh                     1.0512     0.9513    0.9397    1.1760
# susinidum_pbc                    1.0701     0.9345    0.9360    1.2234
# susinidum_mar                    1.0842     0.9223    0.9667    1.2160
# psycom_dum_with_rec2             0.9255     1.0805    0.8938    0.9583
# psycom_dum_study                 0.8423     1.1873    0.7934    0.8941
# freq_cons_dum_5day               0.9871     1.0131    0.9471    1.0287
# cond_oc_dum_2inact               0.9946     1.0055    0.9400    1.0522
# cond_oc_dum_3unemp               1.0491     0.9532    1.0039    1.0963
# susprindum_oh                    1.1321     0.8834    0.9715    1.3191
# susprindum_coc                   1.1931     0.8381    1.0205    1.3950
# susprindum_pbc                   1.2434     0.8042    1.0657    1.4508
# susprindum_mar                   1.2018     0.8321    1.0206    1.4153
# 
# Concordance= 0.706  (se = 0.002 )
# Likelihood ratio test= 5371  on 20 df,   p=<2e-16
# Wald test            = 5053  on 20 df,   p=<2e-16
# Score (logrank) test = 5632  on 20 df,   p=<2e-16,   Robust = 3864  p=<2e-16
# 
#   (Note: the likelihood ratio and score tests assume independence of
#      observations within a cluster, the Wald and robust score tests do not).

summary(iiw_model_after_ph_ba_alt$iiw)
     Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
   0.0643    0.1402    0.5821    1.1179    1.4574 1294.5167 
Code
 #   Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
 # 0.0643    0.1402    0.5821    1.1179    1.4574 1294.5167 

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
invisible("GP intensive ambulatory")

iiw_model_after_ph_gp_ia<-
iiw.weights(Surv(time.lag,time,event)~ 
            cluster(id)+ 
            tr_outcome_rec.lag +
            log_dias_treat_imp_sin_na.lag +
            less_90d_tr1_rec.lag+
            comp_bpsc_y3_severe_rec.lag + 
            policonsumo2.lag + 
            edad_al_ing_1 + 
            ano_nac_corr + 
            susinidum_coc_rec2 +
            susinidum_oh +
            susinidum_pbc +
            susinidum_mar +
            psycom_dum_with_rec2 +
            psycom_dum_study + 
            freq_cons_dum_5day +
            cond_oc_dum_2inact +
            cond_oc_dum_3unemp +
            susprindum_oh +
            susprindum_coc +
            susprindum_pbc +
            susprindum_mar, 
            data= subset(data_mine_miss_restr_proc2, tipo_de_plan_2_mod== levels_tipo_de_plan_2_mod[[2]]) %>% as.data.frame(),
            id= "id",
            time= "time",
            event= "event", #character string indicating which column of the data indicates whether or not a visit occurred. If every row corresponds to a visit, then this column will consist entirely of ones
            maxfu= subset(maxfu_restr_df, subset=  maxfu.id %in% subset(data_mine_miss_restr_proc2, tipo_de_plan_2_mod==levels_tipo_de_plan_2_mod[[2]])$id) %>% as.data.frame(),
            # formulanull = Surv(time.lag,time,event)~ edad_al_ing_1 + 
            #       ano_nac_corr,
            invariant= c("edad_al_ing_1", "ano_nac_corr", "susinidum_coc_rec2", "susinidum_oh", "susinidum_pbc", "susinidum_mar",  "psycom_dum_with_rec2", "psycom_dum_study", "freq_cons_dum_5day", "cond_oc_dum_2inact", "cond_oc_dum_3unemp", "susprindum_oh", "susprindum_coc", "susprindum_pbc", "susprindum_mar"),
            lagvars= c("time", "log_dias_treat_imp_sin_na","tr_outcome_rec", "comp_bpsc_y3_severe_rec", "less_90d_tr1_rec", "policonsumo2"),
            lagfirst= c(2.95082,4.499811/2,0,0,0,0),  #90/30.5 4.499811 es 90 días
            first= T
)

summary(iiw_model_after_ph_gp_ia$m)
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome_rec.lag + 
    log_dias_treat_imp_sin_na.lag + less_90d_tr1_rec.lag + comp_bpsc_y3_severe_rec.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_coc_rec2 + 
    susinidum_oh + susinidum_pbc + susinidum_mar + psycom_dum_with_rec2 + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 15256, number of events= 10260 
   (1237 observations deleted due to missingness)

                                   coef exp(coef)  se(coef) robust se       z
tr_outcome_rec.lag             0.385527  1.470389  0.059938  0.052500   7.343
log_dias_treat_imp_sin_na.lag -0.159892  0.852236  0.005840  0.012235 -13.068
less_90d_tr1_rec.lag          -0.129562  0.878480  0.065487  0.063315  -2.046
comp_bpsc_y3_severe_rec.lag   -0.064406  0.937624  0.057456  0.050548  -1.274
policonsumo2.lag              -0.751697  0.471566  0.023787  0.023197 -32.405
edad_al_ing_1                  0.047348  1.048487  0.004745  0.002975  15.915
ano_nac_corr                   0.050791  1.052102  0.004669  0.002920  17.392
susinidum_coc_rec2             0.141599  1.152114  0.073895  0.055690   2.543
susinidum_oh                   0.140127  1.150420  0.058018  0.039225   3.572
susinidum_pbc                  0.002339  1.002341  0.068236  0.046669   0.050
susinidum_mar                  0.169380  1.184570  0.059120  0.040104   4.224
psycom_dum_with_rec2          -0.002221  0.997781  0.019789  0.013096  -0.170
psycom_dum_study              -0.285588  0.751572  0.032376  0.027958 -10.215
freq_cons_dum_5day             0.009801  1.009849  0.020311  0.013832   0.709
cond_oc_dum_2inact            -0.061440  0.940410  0.030576  0.019679  -3.122
cond_oc_dum_3unemp            -0.018074  0.982089  0.021957  0.014496  -1.247
susprindum_oh                 -0.106767  0.898735  0.087977  0.052865  -2.020
susprindum_coc                 0.029272  1.029704  0.088615  0.053023   0.552
susprindum_pbc                 0.039693  1.040491  0.086621  0.051892   0.765
susprindum_mar                 0.006736  1.006759  0.095242  0.059612   0.113
                              Pr(>|z|)    
tr_outcome_rec.lag            2.08e-13 ***
log_dias_treat_imp_sin_na.lag  < 2e-16 ***
less_90d_tr1_rec.lag          0.040727 *  
comp_bpsc_y3_severe_rec.lag   0.202613    
policonsumo2.lag               < 2e-16 ***
edad_al_ing_1                  < 2e-16 ***
ano_nac_corr                   < 2e-16 ***
susinidum_coc_rec2            0.011003 *  
susinidum_oh                  0.000354 ***
susinidum_pbc                 0.960035    
susinidum_mar                 2.41e-05 ***
psycom_dum_with_rec2          0.865316    
psycom_dum_study               < 2e-16 ***
freq_cons_dum_5day            0.478583    
cond_oc_dum_2inact            0.001796 ** 
cond_oc_dum_3unemp            0.212480    
susprindum_oh                 0.043422 *  
susprindum_coc                0.580909    
susprindum_pbc                0.444322    
susprindum_mar                0.910028    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome_rec.lag               1.4704     0.6801    1.3266    1.6297
log_dias_treat_imp_sin_na.lag    0.8522     1.1734    0.8320    0.8729
less_90d_tr1_rec.lag             0.8785     1.1383    0.7760    0.9945
comp_bpsc_y3_severe_rec.lag      0.9376     1.0665    0.8492    1.0353
policonsumo2.lag                 0.4716     2.1206    0.4506    0.4935
edad_al_ing_1                    1.0485     0.9538    1.0424    1.0546
ano_nac_corr                     1.0521     0.9505    1.0461    1.0581
susinidum_coc_rec2               1.1521     0.8680    1.0330    1.2850
susinidum_oh                     1.1504     0.8692    1.0653    1.2424
susinidum_pbc                    1.0023     0.9977    0.9147    1.0983
susinidum_mar                    1.1846     0.8442    1.0950    1.2814
psycom_dum_with_rec2             0.9978     1.0022    0.9725    1.0237
psycom_dum_study                 0.7516     1.3305    0.7115    0.7939
freq_cons_dum_5day               1.0098     0.9902    0.9828    1.0376
cond_oc_dum_2inact               0.9404     1.0634    0.9048    0.9774
cond_oc_dum_3unemp               0.9821     1.0182    0.9546    1.0104
susprindum_oh                    0.8987     1.1127    0.8103    0.9969
susprindum_coc                   1.0297     0.9712    0.9281    1.1425
susprindum_pbc                   1.0405     0.9611    0.9399    1.1519
susprindum_mar                   1.0068     0.9933    0.8957    1.1315

Concordance= 0.633  (se = 0.002 )
Likelihood ratio test= 2140  on 20 df,   p=<2e-16
Wald test            = 2876  on 20 df,   p=<2e-16
Score (logrank) test = 2305  on 20 df,   p=<2e-16,   Robust = 2637  p=<2e-16

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
Code
# Call:
# coxph(formula = Surv(time.lag, time, event) ~ tr_outcome_rec.lag + 
#     log_dias_treat_imp_sin_na.lag + less_90d_tr1_rec.lag + comp_bpsc_y3_severe_rec.lag + 
#     policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_coc_rec2 + 
#     susinidum_oh + susinidum_pbc + susinidum_mar + psycom_dum_with_rec2 + 
#     psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
#     cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
#     susprindum_mar, data = datacox, cluster = id)
# 
#   n= 15256, number of events= 10260 
#    (1237 observations deleted due to missingness)
# 
#                                    coef exp(coef)  se(coef) robust se       z Pr(>|z|)    
# tr_outcome_rec.lag             0.194561  1.214778  0.060033  0.041647   4.672 2.99e-06 ***
# log_dias_treat_imp_sin_na.lag  0.037861  1.038587  0.012193  0.011379   3.327 0.000877 ***
# less_90d_tr1_rec.lag           0.109752  1.116001  0.068389  0.049698   2.208 0.027217 *  
# comp_bpsc_y3_severe_rec.lag    0.023291  1.023564  0.058271  0.040852   0.570 0.568590    
# policonsumo2.lag              -0.865899  0.420673  0.023128  0.019879 -43.559  < 2e-16 ***
# edad_al_ing_1                  0.037553  1.038267  0.004720  0.002930  12.819  < 2e-16 ***
# ano_nac_corr                   0.042753  1.043681  0.004647  0.002895  14.771  < 2e-16 ***
# susinidum_coc_rec2             0.131486  1.140522  0.074109  0.050774   2.590 0.009607 ** 
# susinidum_oh                   0.145019  1.156062  0.058176  0.037817   3.835 0.000126 ***
# susinidum_pbc                 -0.007182  0.992844  0.068385  0.045736  -0.157 0.875226    
# susinidum_mar                  0.175201  1.191485  0.059249  0.038641   4.534 5.79e-06 ***
# psycom_dum_with_rec2           0.019278  1.019465  0.019842  0.011894   1.621 0.105056    
# psycom_dum_study              -0.242411  0.784734  0.032301  0.026523  -9.140  < 2e-16 ***
# freq_cons_dum_5day             0.018721  1.018897  0.020325  0.013005   1.439 0.150020    
# cond_oc_dum_2inact            -0.059131  0.942583  0.030582  0.019607  -3.016 0.002562 ** 
# cond_oc_dum_3unemp             0.008273  1.008307  0.021893  0.013962   0.593 0.553497    
# susprindum_oh                 -0.104867  0.900444  0.088141  0.051923  -2.020 0.043417 *  
# susprindum_coc                 0.047539  1.048688  0.088742  0.052255   0.910 0.362951    
# susprindum_pbc                 0.056574  1.058205  0.086757  0.051073   1.108 0.267988    
# susprindum_mar                 0.058619  1.060371  0.095320  0.056059   1.046 0.295721    
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# 
#                               exp(coef) exp(-coef) lower .95 upper .95
# tr_outcome_rec.lag               1.2148     0.8232    1.1196    1.3181
# log_dias_treat_imp_sin_na.lag    1.0386     0.9628    1.0157    1.0620
# less_90d_tr1_rec.lag             1.1160     0.8961    1.0124    1.2302
# comp_bpsc_y3_severe_rec.lag      1.0236     0.9770    0.9448    1.1089
# policonsumo2.lag                 0.4207     2.3771    0.4046    0.4374
# edad_al_ing_1                    1.0383     0.9631    1.0323    1.0442
# ano_nac_corr                     1.0437     0.9581    1.0378    1.0496
# susinidum_coc_rec2               1.1405     0.8768    1.0325    1.2599
# susinidum_oh                     1.1561     0.8650    1.0735    1.2450
# susinidum_pbc                    0.9928     1.0072    0.9077    1.0860
# susinidum_mar                    1.1915     0.8393    1.1046    1.2852
# psycom_dum_with_rec2             1.0195     0.9809    0.9960    1.0435
# psycom_dum_study                 0.7847     1.2743    0.7450    0.8266
# freq_cons_dum_5day               1.0189     0.9815    0.9933    1.0452
# cond_oc_dum_2inact               0.9426     1.0609    0.9070    0.9795
# cond_oc_dum_3unemp               1.0083     0.9918    0.9811    1.0363
# susprindum_oh                    0.9004     1.1106    0.8133    0.9969
# susprindum_coc                   1.0487     0.9536    0.9466    1.1618
# susprindum_pbc                   1.0582     0.9450    0.9574    1.1696
# susprindum_mar                   1.0604     0.9431    0.9500    1.1835
# 
# Concordance= 0.625  (se = 0.002 )
# Likelihood ratio test= 1595  on 20 df,   p=<2e-16
# Wald test            = 2810  on 20 df,   p=<2e-16
# Score (logrank) test = 1706  on 20 df,   p=<2e-16,   Robust = 2370  p=<2e-16
# 
#   (Note: the likelihood ratio and score tests assume independence of
#      observations within a cluster, the Wald and robust score tests do not).

summary(iiw_model_after_ph_gp_ia$iiw.weight)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 0.2832  0.7168  1.1537  1.4504  2.0894  5.3832 
Code
#  Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
# 0.5251  0.9191  1.2296  1.4771  1.9988  3.7976 


iiw_model_after_ph_gp_ia_alt<-
iiw.weights(Surv(time.lag,time,event)~ 
            cluster(id)+ 
            tr_outcome_rec.lag +
            log_dias_treat_imp_sin_na.lag +
            less_90d_tr1_rec.lag+
            comp_bpsc_y3_severe_rec.lag + 
            policonsumo2.lag + 
            edad_al_ing_1 + 
            ano_nac_corr + 
            susinidum_coc_rec2 +
            susinidum_oh +
            susinidum_pbc +
            susinidum_mar +
            psycom_dum_with_rec2 +
            psycom_dum_study + 
            freq_cons_dum_5day +
            cond_oc_dum_2inact +
            cond_oc_dum_3unemp +
            susprindum_oh +
            susprindum_coc +
            susprindum_pbc +
            susprindum_mar, 
            data= subset(data_mine_miss_restr_proc2, tipo_de_plan_2_mod== levels_tipo_de_plan_2_mod[[2]]) %>% as.data.frame(),
            id= "id",
            time= "time",
            event= "event", #character string indicating which column of the data indicates whether or not a visit occurred. If every row corresponds to a visit, then this column will consist entirely of ones
            maxfu= subset(maxfu_restr_df, subset=  maxfu.id %in% subset(data_mine_miss_restr_proc2, tipo_de_plan_2_mod==levels_tipo_de_plan_2_mod[[2]])$id) %>% as.data.frame(),
            # formulanull = Surv(time.lag,time,event)~ edad_al_ing_1 + 
            #       ano_nac_corr,
            invariant= c("edad_al_ing_1", "ano_nac_corr", "susinidum_coc_rec2", "susinidum_oh", "susinidum_pbc", "susinidum_mar",  "psycom_dum_with_rec2", "psycom_dum_study", "freq_cons_dum_5day", "cond_oc_dum_2inact", "cond_oc_dum_3unemp", "susprindum_oh", "susprindum_coc", "susprindum_pbc", "susprindum_mar"),
            lagvars= c("time", "log_dias_treat_imp_sin_na","tr_outcome_rec", "comp_bpsc_y3_severe_rec", "less_90d_tr1_rec", "policonsumo2"),
            lagfirst= c(2.95082,4.499811,1,1,1,1),  #90/30.5 4.499811 es 90 días
            first= T
)

summary(iiw_model_after_ph_gp_ia_alt$m)
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome_rec.lag + 
    log_dias_treat_imp_sin_na.lag + less_90d_tr1_rec.lag + comp_bpsc_y3_severe_rec.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_coc_rec2 + 
    susinidum_oh + susinidum_pbc + susinidum_mar + psycom_dum_with_rec2 + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 15256, number of events= 10260 
   (1237 observations deleted due to missingness)

                                   coef exp(coef)  se(coef) robust se      z
tr_outcome_rec.lag             2.014920  7.500129  0.071852  0.068771 29.299
log_dias_treat_imp_sin_na.lag  0.500735  1.649933  0.019681  0.026847 18.652
less_90d_tr1_rec.lag           0.549827  1.732953  0.059743  0.050431 10.903
comp_bpsc_y3_severe_rec.lag    0.433513  1.542667  0.048326  0.045058  9.621
policonsumo2.lag               0.079196  1.082417  0.034122  0.029496  2.685
edad_al_ing_1                  0.107546  1.113542  0.004831  0.004325 24.868
ano_nac_corr                   0.111431  1.117877  0.004759  0.004253 26.199
susinidum_coc_rec2             0.103013  1.108506  0.072414  0.075162  1.371
susinidum_oh                   0.102275  1.107689  0.057331  0.051648  1.980
susinidum_pbc                  0.112886  1.119504  0.067849  0.060463  1.867
susinidum_mar                  0.157963  1.171122  0.058585  0.052701  2.997
psycom_dum_with_rec2          -0.047860  0.953267  0.019768  0.015955 -3.000
psycom_dum_study              -0.281336  0.754775  0.031771  0.031912 -8.816
freq_cons_dum_5day             0.030022  1.030478  0.020274  0.017996  1.668
cond_oc_dum_2inact            -0.037308  0.963380  0.030618  0.026755 -1.394
cond_oc_dum_3unemp             0.031426  1.031925  0.021888  0.019230  1.634
susprindum_oh                 -0.085834  0.917747  0.087841  0.069617 -1.233
susprindum_coc                -0.021955  0.978285  0.088512  0.070315 -0.312
susprindum_pbc                 0.017908  1.018069  0.086516  0.068698  0.261
susprindum_mar                -0.053078  0.948306  0.095181  0.077342 -0.686
                              Pr(>|z|)    
tr_outcome_rec.lag             < 2e-16 ***
log_dias_treat_imp_sin_na.lag  < 2e-16 ***
less_90d_tr1_rec.lag           < 2e-16 ***
comp_bpsc_y3_severe_rec.lag    < 2e-16 ***
policonsumo2.lag               0.00725 ** 
edad_al_ing_1                  < 2e-16 ***
ano_nac_corr                   < 2e-16 ***
susinidum_coc_rec2             0.17051    
susinidum_oh                   0.04768 *  
susinidum_pbc                  0.06190 .  
susinidum_mar                  0.00272 ** 
psycom_dum_with_rec2           0.00270 ** 
psycom_dum_study               < 2e-16 ***
freq_cons_dum_5day             0.09525 .  
cond_oc_dum_2inact             0.16320    
cond_oc_dum_3unemp             0.10222    
susprindum_oh                  0.21760    
susprindum_coc                 0.75487    
susprindum_pbc                 0.79435    
susprindum_mar                 0.49254    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome_rec.lag               7.5001     0.1333    6.5544    8.5824
log_dias_treat_imp_sin_na.lag    1.6499     0.6061    1.5654    1.7391
less_90d_tr1_rec.lag             1.7330     0.5770    1.5699    1.9130
comp_bpsc_y3_severe_rec.lag      1.5427     0.6482    1.4123    1.6851
policonsumo2.lag                 1.0824     0.9239    1.0216    1.1468
edad_al_ing_1                    1.1135     0.8980    1.1041    1.1230
ano_nac_corr                     1.1179     0.8946    1.1086    1.1272
susinidum_coc_rec2               1.1085     0.9021    0.9567    1.2844
susinidum_oh                     1.1077     0.9028    1.0010    1.2257
susinidum_pbc                    1.1195     0.8933    0.9944    1.2604
susinidum_mar                    1.1711     0.8539    1.0562    1.2986
psycom_dum_with_rec2             0.9533     1.0490    0.9239    0.9835
psycom_dum_study                 0.7548     1.3249    0.7090    0.8035
freq_cons_dum_5day               1.0305     0.9704    0.9948    1.0675
cond_oc_dum_2inact               0.9634     1.0380    0.9142    1.0152
cond_oc_dum_3unemp               1.0319     0.9691    0.9938    1.0716
susprindum_oh                    0.9177     1.0896    0.8007    1.0519
susprindum_coc                   0.9783     1.0222    0.8523    1.1228
susprindum_pbc                   1.0181     0.9823    0.8898    1.1648
susprindum_mar                   0.9483     1.0545    0.8149    1.1035

Concordance= 0.707  (se = 0.002 )
Likelihood ratio test= 5753  on 20 df,   p=<2e-16
Wald test            = 5237  on 20 df,   p=<2e-16
Score (logrank) test = 5780  on 20 df,   p=<2e-16,   Robust = 4219  p=<2e-16

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
Code
# Call:
# coxph(formula = Surv(time.lag, time, event) ~ tr_outcome_rec.lag + 
#     log_dias_treat_imp_sin_na.lag + less_90d_tr1_rec.lag + comp_bpsc_y3_severe_rec.lag + 
#     policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_coc_rec2 + 
#     susinidum_oh + susinidum_pbc + susinidum_mar + psycom_dum_with_rec2 + 
#     psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
#     cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
#     susprindum_mar, data = datacox, cluster = id)
# 
#   n= 15256, number of events= 10260 
#    (1237 observations deleted due to missingness)
# 
#                                    coef exp(coef)  se(coef) robust se      z Pr(>|z|)    
# tr_outcome_rec.lag             2.014920  7.500129  0.071852  0.068771 29.299  < 2e-16 ***
# log_dias_treat_imp_sin_na.lag  0.500735  1.649933  0.019681  0.026847 18.652  < 2e-16 ***
# less_90d_tr1_rec.lag           0.549827  1.732953  0.059743  0.050431 10.903  < 2e-16 ***
# comp_bpsc_y3_severe_rec.lag    0.433513  1.542667  0.048326  0.045058  9.621  < 2e-16 ***
# policonsumo2.lag               0.079196  1.082417  0.034122  0.029496  2.685  0.00725 ** 
# edad_al_ing_1                  0.107546  1.113542  0.004831  0.004325 24.868  < 2e-16 ***
# ano_nac_corr                   0.111431  1.117877  0.004759  0.004253 26.199  < 2e-16 ***
# susinidum_coc_rec2             0.103013  1.108506  0.072414  0.075162  1.371  0.17051    
# susinidum_oh                   0.102275  1.107689  0.057331  0.051648  1.980  0.04768 *  
# susinidum_pbc                  0.112886  1.119504  0.067849  0.060463  1.867  0.06190 .  
# susinidum_mar                  0.157963  1.171122  0.058585  0.052701  2.997  0.00272 ** 
# psycom_dum_with_rec2          -0.047860  0.953267  0.019768  0.015955 -3.000  0.00270 ** 
# psycom_dum_study              -0.281336  0.754775  0.031771  0.031912 -8.816  < 2e-16 ***
# freq_cons_dum_5day             0.030022  1.030478  0.020274  0.017996  1.668  0.09525 .  
# cond_oc_dum_2inact            -0.037308  0.963380  0.030618  0.026755 -1.394  0.16320    
# cond_oc_dum_3unemp             0.031426  1.031925  0.021888  0.019230  1.634  0.10222    
# susprindum_oh                 -0.085834  0.917747  0.087841  0.069617 -1.233  0.21760    
# susprindum_coc                -0.021955  0.978285  0.088512  0.070315 -0.312  0.75487    
# susprindum_pbc                 0.017908  1.018069  0.086516  0.068698  0.261  0.79435    
# susprindum_mar                -0.053078  0.948306  0.095181  0.077342 -0.686  0.49254    
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# 
#                               exp(coef) exp(-coef) lower .95 upper .95
# tr_outcome_rec.lag               7.5001     0.1333    6.5544    8.5824
# log_dias_treat_imp_sin_na.lag    1.6499     0.6061    1.5654    1.7391
# less_90d_tr1_rec.lag             1.7330     0.5770    1.5699    1.9130
# comp_bpsc_y3_severe_rec.lag      1.5427     0.6482    1.4123    1.6851
# policonsumo2.lag                 1.0824     0.9239    1.0216    1.1468
# edad_al_ing_1                    1.1135     0.8980    1.1041    1.1230
# ano_nac_corr                     1.1179     0.8946    1.1086    1.1272
# susinidum_coc_rec2               1.1085     0.9021    0.9567    1.2844
# susinidum_oh                     1.1077     0.9028    1.0010    1.2257
# susinidum_pbc                    1.1195     0.8933    0.9944    1.2604
# susinidum_mar                    1.1711     0.8539    1.0562    1.2986
# psycom_dum_with_rec2             0.9533     1.0490    0.9239    0.9835
# psycom_dum_study                 0.7548     1.3249    0.7090    0.8035
# freq_cons_dum_5day               1.0305     0.9704    0.9948    1.0675
# cond_oc_dum_2inact               0.9634     1.0380    0.9142    1.0152
# cond_oc_dum_3unemp               1.0319     0.9691    0.9938    1.0716
# susprindum_oh                    0.9177     1.0896    0.8007    1.0519
# susprindum_coc                   0.9783     1.0222    0.8523    1.1228
# susprindum_pbc                   1.0181     0.9823    0.8898    1.1648
# susprindum_mar                   0.9483     1.0545    0.8149    1.1035
# 
# Concordance= 0.707  (se = 0.002 )
# Likelihood ratio test= 5753  on 20 df,   p=<2e-16
# Wald test            = 5237  on 20 df,   p=<2e-16
# Score (logrank) test = 5780  on 20 df,   p=<2e-16,   Robust = 4219  p=<2e-16
# 
#   (Note: the likelihood ratio and score tests assume independence of
#      observations within a cluster, the Wald and robust score tests do not).

summary(iiw_model_after_ph_gp_ia_alt$iiw.weight)
     Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
   0.0678    0.1663    0.5251    1.4332    1.3754 1183.8688 
Code
#  Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
# 0.0678    0.1663    0.5251    1.4332    1.3754 1183.8688 

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
invisible("GP residential")

iiw_model_after_ph_gp_res<-
iiw.weights(Surv(time.lag,time,event)~ 
            cluster(id)+ 
            tr_outcome_rec.lag +
            log_dias_treat_imp_sin_na.lag +
            less_90d_tr1_rec.lag+
            comp_bpsc_y3_severe_rec.lag + 
            policonsumo2.lag + 
            edad_al_ing_1 + 
            ano_nac_corr + 
            susinidum_coc_rec2 +
            susinidum_oh +
            susinidum_pbc +
            susinidum_mar +
            psycom_dum_with_rec2 +
            psycom_dum_study + 
            freq_cons_dum_5day +
            cond_oc_dum_2inact +
            cond_oc_dum_3unemp +
            susprindum_oh +
            susprindum_coc +
            susprindum_pbc +
            susprindum_mar, 
            data= subset(data_mine_miss_restr_proc2, tipo_de_plan_2_mod== levels_tipo_de_plan_2_mod[[3]]) %>% as.data.frame(),
            id= "id",
            time= "time",
            event= "event", #character string indicating which column of the data indicates whether or not a visit occurred. If every row corresponds to a visit, then this column will consist entirely of ones
            maxfu= subset(maxfu_restr_df, subset=  maxfu.id %in% subset(data_mine_miss_restr_proc2, tipo_de_plan_2_mod==levels_tipo_de_plan_2_mod[[3]])$id) %>% as.data.frame(),
            # formulanull = Surv(time.lag,time,event)~ edad_al_ing_1 + 
            #       ano_nac_corr,
            invariant= c("edad_al_ing_1", "ano_nac_corr", "susinidum_coc_rec2", "susinidum_oh", "susinidum_pbc", "susinidum_mar",  "psycom_dum_with_rec2", "psycom_dum_study", "freq_cons_dum_5day", "cond_oc_dum_2inact", "cond_oc_dum_3unemp", "susprindum_oh", "susprindum_coc", "susprindum_pbc", "susprindum_mar"),
            lagvars= c("time", "log_dias_treat_imp_sin_na","tr_outcome_rec", "comp_bpsc_y3_severe_rec", "less_90d_tr1_rec", "policonsumo2"),
            lagfirst= c(2.95082,4.499811/2,0,0,0,0),  #90/30.5 4.499811 es 90 días
            first= T
)

summary(iiw_model_after_ph_gp_res$m)
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome_rec.lag + 
    log_dias_treat_imp_sin_na.lag + less_90d_tr1_rec.lag + comp_bpsc_y3_severe_rec.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_coc_rec2 + 
    susinidum_oh + susinidum_pbc + susinidum_mar + psycom_dum_with_rec2 + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 6745, number of events= 4567 
   (637 observations deleted due to missingness)

                                   coef exp(coef)  se(coef) robust se       z
tr_outcome_rec.lag             0.128188  1.136767  0.101990  0.086925   1.475
log_dias_treat_imp_sin_na.lag -0.085496  0.918057  0.008798  0.011288  -7.574
less_90d_tr1_rec.lag          -0.088387  0.915407  0.103118  0.084266  -1.049
comp_bpsc_y3_severe_rec.lag    0.175771  1.192165  0.082749  0.081533   2.156
policonsumo2.lag              -0.750680  0.472045  0.036058  0.034731 -21.614
edad_al_ing_1                  0.028939  1.029361  0.007348  0.004632   6.248
ano_nac_corr                   0.032926  1.033474  0.007304  0.004592   7.170
susinidum_coc_rec2             0.183915  1.201913  0.125673  0.096940   1.897
susinidum_oh                   0.138489  1.148536  0.094577  0.076211   1.817
susinidum_pbc                  0.032443  1.032975  0.101841  0.081788   0.397
susinidum_mar                  0.118222  1.125494  0.094812  0.076811   1.539
psycom_dum_with_rec2           0.056826  1.058472  0.029195  0.019809   2.869
psycom_dum_study              -0.207341  0.812742  0.048764  0.037096  -5.589
freq_cons_dum_5day            -0.027696  0.972684  0.034087  0.023537  -1.177
cond_oc_dum_2inact             0.091545  1.095866  0.055552  0.040162   2.279
cond_oc_dum_3unemp             0.107838  1.113867  0.045740  0.031919   3.379
susprindum_oh                 -0.058676  0.943012  0.158396  0.098428  -0.596
susprindum_coc                 0.091597  1.095923  0.159134  0.098627   0.929
susprindum_pbc                 0.039675  1.040473  0.154505  0.094983   0.418
susprindum_mar                -0.075467  0.927310  0.178402  0.113375  -0.666
                              Pr(>|z|)    
tr_outcome_rec.lag            0.140295    
log_dias_treat_imp_sin_na.lag 3.62e-14 ***
less_90d_tr1_rec.lag          0.294224    
comp_bpsc_y3_severe_rec.lag   0.031096 *  
policonsumo2.lag               < 2e-16 ***
edad_al_ing_1                 4.17e-10 ***
ano_nac_corr                  7.52e-13 ***
susinidum_coc_rec2            0.057802 .  
susinidum_oh                  0.069192 .  
susinidum_pbc                 0.691608    
susinidum_mar                 0.123774    
psycom_dum_with_rec2          0.004121 ** 
psycom_dum_study              2.28e-08 ***
freq_cons_dum_5day            0.239318    
cond_oc_dum_2inact            0.022644 *  
cond_oc_dum_3unemp            0.000729 ***
susprindum_oh                 0.551086    
susprindum_coc                0.353037    
susprindum_pbc                0.676161    
susprindum_mar                0.505641    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome_rec.lag               1.1368     0.8797    0.9587    1.3479
log_dias_treat_imp_sin_na.lag    0.9181     1.0893    0.8980    0.9386
less_90d_tr1_rec.lag             0.9154     1.0924    0.7760    1.0798
comp_bpsc_y3_severe_rec.lag      1.1922     0.8388    1.0161    1.3987
policonsumo2.lag                 0.4720     2.1184    0.4410    0.5053
edad_al_ing_1                    1.0294     0.9715    1.0201    1.0387
ano_nac_corr                     1.0335     0.9676    1.0242    1.0428
susinidum_coc_rec2               1.2019     0.8320    0.9939    1.4534
susinidum_oh                     1.1485     0.8707    0.9892    1.3336
susinidum_pbc                    1.0330     0.9681    0.8800    1.2126
susinidum_mar                    1.1255     0.8885    0.9682    1.3084
psycom_dum_with_rec2             1.0585     0.9448    1.0182    1.1004
psycom_dum_study                 0.8127     1.2304    0.7557    0.8740
freq_cons_dum_5day               0.9727     1.0281    0.9288    1.0186
cond_oc_dum_2inact               1.0959     0.9125    1.0129    1.1856
cond_oc_dum_3unemp               1.1139     0.8978    1.0463    1.1858
susprindum_oh                    0.9430     1.0604    0.7776    1.1437
susprindum_coc                   1.0959     0.9125    0.9033    1.3296
susprindum_pbc                   1.0405     0.9611    0.8637    1.2534
susprindum_mar                   0.9273     1.0784    0.7425    1.1581

Concordance= 0.605  (se = 0.003 )
Likelihood ratio test= 695.9  on 20 df,   p=<2e-16
Wald test            = 1085  on 20 df,   p=<2e-16
Score (logrank) test = 743.2  on 20 df,   p=<2e-16,   Robust = 996.3  p=<2e-16

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
Code
# Call:
# coxph(formula = Surv(time.lag, time, event) ~ tr_outcome_rec.lag + 
#     log_dias_treat_imp_sin_na.lag + less_90d_tr1_rec.lag + comp_bpsc_y3_severe_rec.lag + 
#     policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_coc_rec2 + 
#     susinidum_oh + susinidum_pbc + susinidum_mar + psycom_dum_with_rec2 + 
#     psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
#     cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
#     susprindum_mar, data = datacox, cluster = id)
# 
#   n= 6745, number of events= 4567 
#    (637 observations deleted due to missingness)
# 
#                                    coef exp(coef)  se(coef) robust se       z Pr(>|z|)    
# tr_outcome_rec.lag            -0.039009  0.961742  0.105701  0.074349  -0.525 0.599808    
# log_dias_treat_imp_sin_na.lag  0.078977  1.082180  0.015647  0.013905   5.680 1.35e-08 ***
# less_90d_tr1_rec.lag           0.150856  1.162829  0.107295  0.080559   1.873 0.061122 .  
# comp_bpsc_y3_severe_rec.lag    0.353959  1.424696  0.090172  0.070675   5.008 5.49e-07 ***
# policonsumo2.lag              -0.837956  0.432594  0.035119  0.032878 -25.487  < 2e-16 ***
# edad_al_ing_1                  0.025965  1.026305  0.007319  0.004690   5.536 3.09e-08 ***
# ano_nac_corr                   0.030994  1.031479  0.007283  0.004684   6.617 3.66e-11 ***
# susinidum_coc_rec2             0.160851  1.174510  0.124649  0.094183   1.708 0.087664 .  
# susinidum_oh                   0.114160  1.120931  0.094181  0.074294   1.537 0.124391    
# susinidum_pbc                  0.009529  1.009575  0.101467  0.080150   0.119 0.905358    
# susinidum_mar                  0.103063  1.108562  0.094427  0.074787   1.378 0.168173    
# psycom_dum_with_rec2           0.071582  1.074206  0.029128  0.019756   3.623 0.000291 ***
# psycom_dum_study              -0.187465  0.829058  0.048601  0.037539  -4.994 5.92e-07 ***
# freq_cons_dum_5day            -0.017574  0.982579  0.034084  0.023154  -0.759 0.447831    
# cond_oc_dum_2inact             0.102551  1.107994  0.055537  0.038242   2.682 0.007327 ** 
# cond_oc_dum_3unemp             0.099801  1.104951  0.045743  0.031127   3.206 0.001345 ** 
# susprindum_oh                 -0.030306  0.970149  0.158516  0.107214  -0.283 0.777431    
# susprindum_coc                 0.111599  1.118064  0.159135  0.107984   1.033 0.301381    
# susprindum_pbc                 0.080561  1.083895  0.154488  0.104564   0.770 0.441033    
# susprindum_mar                 0.064293  1.066405  0.177941  0.113070   0.569 0.569619    
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# 
#                               exp(coef) exp(-coef) lower .95 upper .95
# tr_outcome_rec.lag               0.9617     1.0398    0.8313    1.1126
# log_dias_treat_imp_sin_na.lag    1.0822     0.9241    1.0531    1.1121
# less_90d_tr1_rec.lag             1.1628     0.8600    0.9930    1.3617
# comp_bpsc_y3_severe_rec.lag      1.4247     0.7019    1.2404    1.6364
# policonsumo2.lag                 0.4326     2.3116    0.4056    0.4614
# edad_al_ing_1                    1.0263     0.9744    1.0169    1.0358
# ano_nac_corr                     1.0315     0.9695    1.0221    1.0410
# susinidum_coc_rec2               1.1745     0.8514    0.9765    1.4126
# susinidum_oh                     1.1209     0.8921    0.9690    1.2966
# susinidum_pbc                    1.0096     0.9905    0.8628    1.1813
# susinidum_mar                    1.1086     0.9021    0.9574    1.2836
# psycom_dum_with_rec2             1.0742     0.9309    1.0334    1.1166
# psycom_dum_study                 0.8291     1.2062    0.7702    0.8924
# freq_cons_dum_5day               0.9826     1.0177    0.9390    1.0282
# cond_oc_dum_2inact               1.1080     0.9025    1.0280    1.1942
# cond_oc_dum_3unemp               1.1050     0.9050    1.0396    1.1745
# susprindum_oh                    0.9701     1.0308    0.7863    1.1970
# susprindum_coc                   1.1181     0.8944    0.9048    1.3816
# susprindum_pbc                   1.0839     0.9226    0.8830    1.3304
# susprindum_mar                   1.0664     0.9377    0.8544    1.3310
# 
# Concordance= 0.618  (se = 0.003 )
# Likelihood ratio test= 644.2  on 20 df,   p=<2e-16
# Wald test            = 1048  on 20 df,   p=<2e-16
# Score (logrank) test = 689.3  on 20 df,   p=<2e-16,   Robust = 950.3  p=<2e-16
# 
#   (Note: the likelihood ratio and score tests assume independence of
#      observations within a cluster, the Wald and robust score tests do not).

summary(iiw_model_after_ph_gp_res$iiw.weight)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 0.3321  0.7511  1.1708  1.3145  1.8355  3.2183 
Code
 #   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 # 0.5131  0.8729  1.2617  1.3565  1.7689  6.7300 



iiw_model_after_ph_gp_res_alt<-
iiw.weights(Surv(time.lag,time,event)~ 
            cluster(id)+ 
            tr_outcome_rec.lag +
            log_dias_treat_imp_sin_na.lag +
            less_90d_tr1_rec.lag+
            comp_bpsc_y3_severe_rec.lag + 
            policonsumo2.lag + 
            edad_al_ing_1 + 
            ano_nac_corr + 
            susinidum_coc_rec2 +
            susinidum_oh +
            susinidum_pbc +
            susinidum_mar +
            psycom_dum_with_rec2 +
            psycom_dum_study + 
            freq_cons_dum_5day +
            cond_oc_dum_2inact +
            cond_oc_dum_3unemp +
            susprindum_oh +
            susprindum_coc +
            susprindum_pbc +
            susprindum_mar, 
            data= subset(data_mine_miss_restr_proc2, tipo_de_plan_2_mod== levels_tipo_de_plan_2_mod[[3]]) %>% as.data.frame(),
            id= "id",
            time= "time",
            event= "event", #character string indicating which column of the data indicates whether or not a visit occurred. If every row corresponds to a visit, then this column will consist entirely of ones
            maxfu= subset(maxfu_restr_df, subset=  maxfu.id %in% subset(data_mine_miss_restr_proc2, tipo_de_plan_2_mod==levels_tipo_de_plan_2_mod[[3]])$id) %>% as.data.frame(),
            # formulanull = Surv(time.lag,time,event)~ edad_al_ing_1 + 
            #       ano_nac_corr,
            invariant= c("edad_al_ing_1", "ano_nac_corr", "susinidum_coc_rec2", "susinidum_oh", "susinidum_pbc", "susinidum_mar",  "psycom_dum_with_rec2", "psycom_dum_study", "freq_cons_dum_5day", "cond_oc_dum_2inact", "cond_oc_dum_3unemp", "susprindum_oh", "susprindum_coc", "susprindum_pbc", "susprindum_mar"),
            lagvars= c("time", "log_dias_treat_imp_sin_na","tr_outcome_rec", "comp_bpsc_y3_severe_rec", "less_90d_tr1_rec", "policonsumo2"),
            lagfirst= c(2.95082,4.499811,1,1,1,1),  #90/30.5 4.499811 es 90 días
            first= T
)

summary(iiw_model_after_ph_gp_res_alt$m)
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome_rec.lag + 
    log_dias_treat_imp_sin_na.lag + less_90d_tr1_rec.lag + comp_bpsc_y3_severe_rec.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_coc_rec2 + 
    susinidum_oh + susinidum_pbc + susinidum_mar + psycom_dum_with_rec2 + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 6745, number of events= 4567 
   (637 observations deleted due to missingness)

                                   coef exp(coef)  se(coef) robust se      z
tr_outcome_rec.lag             0.981483  2.668410  0.120321  0.103018  9.527
log_dias_treat_imp_sin_na.lag  0.328445  1.388807  0.021738  0.031862 10.308
less_90d_tr1_rec.lag           0.482546  1.620195  0.097638  0.084879  5.685
comp_bpsc_y3_severe_rec.lag    0.940955  2.562426  0.100147  0.088610 10.619
policonsumo2.lag               0.161177  1.174893  0.054428  0.050466  3.194
edad_al_ing_1                  0.060548  1.062418  0.007416  0.006446  9.393
ano_nac_corr                   0.063469  1.065526  0.007369  0.006388  9.936
susinidum_coc_rec2             0.226938  1.254752  0.122208  0.130113  1.744
susinidum_oh                   0.083881  1.087500  0.093000  0.086277  0.972
susinidum_pbc                  0.163264  1.177348  0.100532  0.094265  1.732
susinidum_mar                  0.107515  1.113507  0.093363  0.086830  1.238
psycom_dum_with_rec2          -0.050914  0.950360  0.029484  0.024602 -2.069
psycom_dum_study              -0.357490  0.699430  0.047553  0.046607 -7.670
freq_cons_dum_5day            -0.012423  0.987654  0.033856  0.029387 -0.423
cond_oc_dum_2inact             0.106389  1.112255  0.055170  0.048125  2.211
cond_oc_dum_3unemp             0.065295  1.067474  0.045224  0.039172  1.667
susprindum_oh                  0.101693  1.107044  0.158048  0.131136  0.775
susprindum_coc                 0.046216  1.047300  0.159165  0.131835  0.351
susprindum_pbc                 0.015162  1.015277  0.154594  0.127081  0.119
susprindum_mar                 0.042951  1.043887  0.177913  0.138399  0.310
                              Pr(>|z|)    
tr_outcome_rec.lag             < 2e-16 ***
log_dias_treat_imp_sin_na.lag  < 2e-16 ***
less_90d_tr1_rec.lag          1.31e-08 ***
comp_bpsc_y3_severe_rec.lag    < 2e-16 ***
policonsumo2.lag                0.0014 ** 
edad_al_ing_1                  < 2e-16 ***
ano_nac_corr                   < 2e-16 ***
susinidum_coc_rec2              0.0811 .  
susinidum_oh                    0.3309    
susinidum_pbc                   0.0833 .  
susinidum_mar                   0.2156    
psycom_dum_with_rec2            0.0385 *  
psycom_dum_study              1.72e-14 ***
freq_cons_dum_5day              0.6725    
cond_oc_dum_2inact              0.0271 *  
cond_oc_dum_3unemp              0.0955 .  
susprindum_oh                   0.4381    
susprindum_coc                  0.7259    
susprindum_pbc                  0.9050    
susprindum_mar                  0.7563    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome_rec.lag               2.6684     0.3748    2.1805    3.2654
log_dias_treat_imp_sin_na.lag    1.3888     0.7200    1.3047    1.4783
less_90d_tr1_rec.lag             1.6202     0.6172    1.3719    1.9134
comp_bpsc_y3_severe_rec.lag      2.5624     0.3903    2.1539    3.0484
policonsumo2.lag                 1.1749     0.8511    1.0642    1.2970
edad_al_ing_1                    1.0624     0.9412    1.0491    1.0759
ano_nac_corr                     1.0655     0.9385    1.0523    1.0790
susinidum_coc_rec2               1.2548     0.7970    0.9723    1.6192
susinidum_oh                     1.0875     0.9195    0.9183    1.2879
susinidum_pbc                    1.1773     0.8494    0.9787    1.4163
susinidum_mar                    1.1135     0.8981    0.9393    1.3201
psycom_dum_with_rec2             0.9504     1.0522    0.9056    0.9973
psycom_dum_study                 0.6994     1.4297    0.6384    0.7663
freq_cons_dum_5day               0.9877     1.0125    0.9324    1.0462
cond_oc_dum_2inact               1.1123     0.8991    1.0121    1.2223
cond_oc_dum_3unemp               1.0675     0.9368    0.9886    1.1527
susprindum_oh                    1.1070     0.9033    0.8561    1.4315
susprindum_coc                   1.0473     0.9548    0.8088    1.3561
susprindum_pbc                   1.0153     0.9850    0.7914    1.3024
susprindum_mar                   1.0439     0.9580    0.7959    1.3692

Concordance= 0.682  (se = 0.003 )
Likelihood ratio test= 1994  on 20 df,   p=<2e-16
Wald test            = 2159  on 20 df,   p=<2e-16
Score (logrank) test = 2003  on 20 df,   p=<2e-16,   Robust = 1726  p=<2e-16

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
Code
# Call:
# coxph(formula = Surv(time.lag, time, event) ~ tr_outcome_rec.lag + 
#     log_dias_treat_imp_sin_na.lag + less_90d_tr1_rec.lag + comp_bpsc_y3_severe_rec.lag + 
#     policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_coc_rec2 + 
#     susinidum_oh + susinidum_pbc + susinidum_mar + psycom_dum_with_rec2 + 
#     psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
#     cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
#     susprindum_mar, data = datacox, cluster = id)
# 
#   n= 6745, number of events= 4567 
#    (637 observations deleted due to missingness)
# 
#                                    coef exp(coef)  se(coef) robust se      z Pr(>|z|)    
# tr_outcome_rec.lag             0.981483  2.668410  0.120321  0.103018  9.527  < 2e-16 ***
# log_dias_treat_imp_sin_na.lag  0.328445  1.388807  0.021738  0.031862 10.308  < 2e-16 ***
# less_90d_tr1_rec.lag           0.482546  1.620195  0.097638  0.084879  5.685 1.31e-08 ***
# comp_bpsc_y3_severe_rec.lag    0.940955  2.562426  0.100147  0.088610 10.619  < 2e-16 ***
# policonsumo2.lag               0.161177  1.174893  0.054428  0.050466  3.194   0.0014 ** 
# edad_al_ing_1                  0.060548  1.062418  0.007416  0.006446  9.393  < 2e-16 ***
# ano_nac_corr                   0.063469  1.065526  0.007369  0.006388  9.936  < 2e-16 ***
# susinidum_coc_rec2             0.226938  1.254752  0.122208  0.130113  1.744   0.0811 .  
# susinidum_oh                   0.083881  1.087500  0.093000  0.086277  0.972   0.3309    
# susinidum_pbc                  0.163264  1.177348  0.100532  0.094265  1.732   0.0833 .  
# susinidum_mar                  0.107515  1.113507  0.093363  0.086830  1.238   0.2156    
# psycom_dum_with_rec2          -0.050914  0.950360  0.029484  0.024602 -2.069   0.0385 *  
# psycom_dum_study              -0.357490  0.699430  0.047553  0.046607 -7.670 1.72e-14 ***
# freq_cons_dum_5day            -0.012423  0.987654  0.033856  0.029387 -0.423   0.6725    
# cond_oc_dum_2inact             0.106389  1.112255  0.055170  0.048125  2.211   0.0271 *  
# cond_oc_dum_3unemp             0.065295  1.067474  0.045224  0.039172  1.667   0.0955 .  
# susprindum_oh                  0.101693  1.107044  0.158048  0.131136  0.775   0.4381    
# susprindum_coc                 0.046216  1.047300  0.159165  0.131835  0.351   0.7259    
# susprindum_pbc                 0.015162  1.015277  0.154594  0.127081  0.119   0.9050    
# susprindum_mar                 0.042951  1.043887  0.177913  0.138399  0.310   0.7563    
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# 
#                               exp(coef) exp(-coef) lower .95 upper .95
# tr_outcome_rec.lag               2.6684     0.3748    2.1805    3.2654
# log_dias_treat_imp_sin_na.lag    1.3888     0.7200    1.3047    1.4783
# less_90d_tr1_rec.lag             1.6202     0.6172    1.3719    1.9134
# comp_bpsc_y3_severe_rec.lag      2.5624     0.3903    2.1539    3.0484
# policonsumo2.lag                 1.1749     0.8511    1.0642    1.2970
# edad_al_ing_1                    1.0624     0.9412    1.0491    1.0759
# ano_nac_corr                     1.0655     0.9385    1.0523    1.0790
# susinidum_coc_rec2               1.2548     0.7970    0.9723    1.6192
# susinidum_oh                     1.0875     0.9195    0.9183    1.2879
# susinidum_pbc                    1.1773     0.8494    0.9787    1.4163
# susinidum_mar                    1.1135     0.8981    0.9393    1.3201
# psycom_dum_with_rec2             0.9504     1.0522    0.9056    0.9973
# psycom_dum_study                 0.6994     1.4297    0.6384    0.7663
# freq_cons_dum_5day               0.9877     1.0125    0.9324    1.0462
# cond_oc_dum_2inact               1.1123     0.8991    1.0121    1.2223
# cond_oc_dum_3unemp               1.0675     0.9368    0.9886    1.1527
# susprindum_oh                    1.1070     0.9033    0.8561    1.4315
# susprindum_coc                   1.0473     0.9548    0.8088    1.3561
# susprindum_pbc                   1.0153     0.9850    0.7914    1.3024
# susprindum_mar                   1.0439     0.9580    0.7959    1.3692
# 
# Concordance= 0.682  (se = 0.003 )
# Likelihood ratio test= 1994  on 20 df,   p=<2e-16
# Wald test            = 2159  on 20 df,   p=<2e-16
# Score (logrank) test = 2003  on 20 df,   p=<2e-16,   Robust = 1726  p=<2e-16
# 
#   (Note: the likelihood ratio and score tests assume independence of
#      observations within a cluster, the Wald and robust score tests do not).


#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
invisible("WO intensive ambulatory")

iiw_model_after_ph_wo_ia<-
iiw.weights(Surv(time.lag,time,event)~ 
            cluster(id)+ 
            tr_outcome_rec.lag +
            log_dias_treat_imp_sin_na.lag +
            less_90d_tr1_rec.lag+
            comp_bpsc_y3_severe_rec.lag + 
            policonsumo2.lag + 
            edad_al_ing_1 + 
            ano_nac_corr + 
            susinidum_coc_rec2 +
            susinidum_oh +
            susinidum_pbc +
            susinidum_mar +
            psycom_dum_with_rec2 +
            psycom_dum_study + 
            freq_cons_dum_5day +
            cond_oc_dum_2inact +
            cond_oc_dum_3unemp +
            susprindum_oh +
            susprindum_coc +
            susprindum_pbc +
            susprindum_mar, 
            data= subset(data_mine_miss_restr_proc2, tipo_de_plan_2_mod== levels_tipo_de_plan_2_mod[[4]]) %>% as.data.frame(),
            id= "id",
            time= "time",
            event= "event", #character string indicating which column of the data indicates whether or not a visit occurred. If every row corresponds to a visit, then this column will consist entirely of ones
            maxfu= subset(maxfu_restr_df, subset=  maxfu.id %in% subset(data_mine_miss_restr_proc2, tipo_de_plan_2_mod==levels_tipo_de_plan_2_mod[[4]])$id) %>% as.data.frame(),
            # formulanull = Surv(time.lag,time,event)~ edad_al_ing_1 + 
            #       ano_nac_corr,
            invariant= c("edad_al_ing_1", "ano_nac_corr", "susinidum_coc_rec2", "susinidum_oh", "susinidum_pbc", "susinidum_mar",  "psycom_dum_with_rec2", "psycom_dum_study", "freq_cons_dum_5day", "cond_oc_dum_2inact", "cond_oc_dum_3unemp", "susprindum_oh", "susprindum_coc", "susprindum_pbc", "susprindum_mar"),
            lagvars= c("time", "log_dias_treat_imp_sin_na","tr_outcome_rec", "comp_bpsc_y3_severe_rec", "less_90d_tr1_rec", "policonsumo2"),
            lagfirst= c(2.95082,4.499811/2,0,0,0,0),  #90/30.5 4.499811 es 90 días
            first= T
)

summary(iiw_model_after_ph_wo_ia$m)
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome_rec.lag + 
    log_dias_treat_imp_sin_na.lag + less_90d_tr1_rec.lag + comp_bpsc_y3_severe_rec.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_coc_rec2 + 
    susinidum_oh + susinidum_pbc + susinidum_mar + psycom_dum_with_rec2 + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 2342, number of events= 1597 
   (163 observations deleted due to missingness)

                                   coef exp(coef)  se(coef) robust se       z
tr_outcome_rec.lag             0.590396  1.804703  0.165929  0.118174   4.996
log_dias_treat_imp_sin_na.lag -0.243294  0.784041  0.022535  0.025535  -9.528
less_90d_tr1_rec.lag          -0.206354  0.813545  0.194073  0.151714  -1.360
comp_bpsc_y3_severe_rec.lag   -0.132974  0.875488  0.164473  0.115583  -1.150
policonsumo2.lag              -0.615544  0.540347  0.061535  0.054363 -11.323
edad_al_ing_1                  0.037924  1.038652  0.012119  0.007974   4.756
ano_nac_corr                   0.042599  1.043519  0.011695  0.007815   5.451
susinidum_coc_rec2            -0.042127  0.958748  0.170050  0.135358  -0.311
susinidum_oh                  -0.025163  0.975151  0.143634  0.096426  -0.261
susinidum_pbc                 -0.020822  0.979393  0.156236  0.108823  -0.191
susinidum_mar                  0.012483  1.012562  0.144243  0.094129   0.133
psycom_dum_with_rec2           0.016660  1.016799  0.049880  0.031631   0.527
psycom_dum_study              -0.275828  0.758943  0.085551  0.072534  -3.803
freq_cons_dum_5day             0.031700  1.032208  0.051667  0.032640   0.971
cond_oc_dum_2inact            -0.018931  0.981247  0.065151  0.042881  -0.441
cond_oc_dum_3unemp            -0.005474  0.994541  0.067826  0.042655  -0.128
susprindum_oh                  0.088216  1.092224  0.183762  0.117491   0.751
susprindum_coc                 0.128627  1.137265  0.186221  0.126652   1.016
susprindum_pbc                 0.100929  1.106199  0.180469  0.123526   0.817
susprindum_mar                 0.086862  1.090746  0.199405  0.140061   0.620
                              Pr(>|z|)    
tr_outcome_rec.lag            5.85e-07 ***
log_dias_treat_imp_sin_na.lag  < 2e-16 ***
less_90d_tr1_rec.lag          0.173783    
comp_bpsc_y3_severe_rec.lag   0.249954    
policonsumo2.lag               < 2e-16 ***
edad_al_ing_1                 1.97e-06 ***
ano_nac_corr                  5.00e-08 ***
susinidum_coc_rec2            0.755627    
susinidum_oh                  0.794129    
susinidum_pbc                 0.848260    
susinidum_mar                 0.894494    
psycom_dum_with_rec2          0.598406    
psycom_dum_study              0.000143 ***
freq_cons_dum_5day            0.331444    
cond_oc_dum_2inact            0.658865    
cond_oc_dum_3unemp            0.897892    
susprindum_oh                 0.452755    
susprindum_coc                0.309823    
susprindum_pbc                0.413890    
susprindum_mar                0.535147    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome_rec.lag               1.8047     0.5541    1.4316    2.2751
log_dias_treat_imp_sin_na.lag    0.7840     1.2754    0.7458    0.8243
less_90d_tr1_rec.lag             0.8135     1.2292    0.6043    1.0953
comp_bpsc_y3_severe_rec.lag      0.8755     1.1422    0.6980    1.0981
policonsumo2.lag                 0.5403     1.8507    0.4857    0.6011
edad_al_ing_1                    1.0387     0.9628    1.0225    1.0550
ano_nac_corr                     1.0435     0.9583    1.0277    1.0596
susinidum_coc_rec2               0.9587     1.0430    0.7353    1.2500
susinidum_oh                     0.9752     1.0255    0.8072    1.1780
susinidum_pbc                    0.9794     1.0210    0.7913    1.2122
susinidum_mar                    1.0126     0.9876    0.8420    1.2177
psycom_dum_with_rec2             1.0168     0.9835    0.9557    1.0818
psycom_dum_study                 0.7589     1.3176    0.6584    0.8749
freq_cons_dum_5day               1.0322     0.9688    0.9682    1.1004
cond_oc_dum_2inact               0.9812     1.0191    0.9021    1.0673
cond_oc_dum_3unemp               0.9945     1.0055    0.9148    1.0813
susprindum_oh                    1.0922     0.9156    0.8676    1.3751
susprindum_coc                   1.1373     0.8793    0.8873    1.4577
susprindum_pbc                   1.1062     0.9040    0.8683    1.4092
susprindum_mar                   1.0907     0.9168    0.8289    1.4353

Concordance= 0.634  (se = 0.006 )
Likelihood ratio test= 334.3  on 20 df,   p=<2e-16
Wald test            = 467.2  on 20 df,   p=<2e-16
Score (logrank) test = 346.7  on 20 df,   p=<2e-16,   Robust = 401.6  p=<2e-16

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
Code
# Call:
# coxph(formula = Surv(time.lag, time, event) ~ tr_outcome_rec.lag + 
#     log_dias_treat_imp_sin_na.lag + less_90d_tr1_rec.lag + comp_bpsc_y3_severe_rec.lag + 
#     policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_coc_rec2 + 
#     susinidum_oh + susinidum_pbc + susinidum_mar + psycom_dum_with_rec2 + 
#     psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
#     cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
#     susprindum_mar, data = datacox, cluster = id)
# 
#   n= 2342, number of events= 1597 
#    (163 observations deleted due to missingness)
# 
#                                     coef  exp(coef)   se(coef)  robust se       z Pr(>|z|)    
# tr_outcome_rec.lag             0.0960769  1.1008437  0.1608893  0.1073787   0.895 0.370922    
# log_dias_treat_imp_sin_na.lag  0.0601846  1.0620325  0.0341951  0.0323800   1.859 0.063070 .  
# less_90d_tr1_rec.lag           0.3920119  1.4799554  0.1955210  0.1410453   2.779 0.005447 ** 
# comp_bpsc_y3_severe_rec.lag    0.0021099  1.0021122  0.1634728  0.1074412   0.020 0.984332    
# policonsumo2.lag              -0.8194113  0.4406910  0.0577374  0.0471515 -17.378  < 2e-16 ***
# edad_al_ing_1                  0.0264262  1.0267785  0.0120769  0.0079482   3.325 0.000885 ***
# ano_nac_corr                   0.0326238  1.0331618  0.0116686  0.0077493   4.210 2.55e-05 ***
# susinidum_coc_rec2             0.0065845  1.0066062  0.1708421  0.1250574   0.053 0.958010    
# susinidum_oh                   0.0354693  1.0361059  0.1442859  0.1006518   0.352 0.724541    
# susinidum_pbc                  0.0354305  1.0360656  0.1568797  0.1101749   0.322 0.747768    
# susinidum_mar                  0.0902561  1.0944546  0.1448738  0.0980071   0.921 0.357095    
# psycom_dum_with_rec2           0.0288659  1.0292865  0.0500612  0.0300631   0.960 0.336967    
# psycom_dum_study              -0.2367838  0.7891619  0.0863122  0.0688161  -3.441 0.000580 ***
# freq_cons_dum_5day             0.0330127  1.0335637  0.0517079  0.0316338   1.044 0.296676    
# cond_oc_dum_2inact            -0.0004428  0.9995573  0.0653684  0.0422072  -0.010 0.991630    
# cond_oc_dum_3unemp            -0.0078465  0.9921842  0.0678832  0.0425011  -0.185 0.853528    
# susprindum_oh                  0.0346128  1.0352188  0.1838352  0.1319090   0.262 0.793014    
# susprindum_coc                 0.1080539  1.1141078  0.1862425  0.1412067   0.765 0.444142    
# susprindum_pbc                 0.1142682  1.1210528  0.1808628  0.1382042   0.827 0.408346    
# susprindum_mar                 0.1072029  1.1131601  0.1989858  0.1539935   0.696 0.486334    
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# 
#                               exp(coef) exp(-coef) lower .95 upper .95
# tr_outcome_rec.lag               1.1008     0.9084    0.8919    1.3587
# log_dias_treat_imp_sin_na.lag    1.0620     0.9416    0.9967    1.1316
# less_90d_tr1_rec.lag             1.4800     0.6757    1.1225    1.9512
# comp_bpsc_y3_severe_rec.lag      1.0021     0.9979    0.8118    1.2370
# policonsumo2.lag                 0.4407     2.2692    0.4018    0.4834
# edad_al_ing_1                    1.0268     0.9739    1.0109    1.0429
# ano_nac_corr                     1.0332     0.9679    1.0176    1.0490
# susinidum_coc_rec2               1.0066     0.9934    0.7878    1.2862
# susinidum_oh                     1.0361     0.9652    0.8506    1.2621
# susinidum_pbc                    1.0361     0.9652    0.8348    1.2858
# susinidum_mar                    1.0945     0.9137    0.9032    1.3262
# psycom_dum_with_rec2             1.0293     0.9715    0.9704    1.0918
# psycom_dum_study                 0.7892     1.2672    0.6896    0.9031
# freq_cons_dum_5day               1.0336     0.9675    0.9714    1.0997
# cond_oc_dum_2inact               0.9996     1.0004    0.9202    1.0858
# cond_oc_dum_3unemp               0.9922     1.0079    0.9129    1.0784
# susprindum_oh                    1.0352     0.9660    0.7994    1.3406
# susprindum_coc                   1.1141     0.8976    0.8448    1.4693
# susprindum_pbc                   1.1211     0.8920    0.8550    1.4698
# susprindum_mar                   1.1132     0.8983    0.8231    1.5053
# 
# Concordance= 0.627  (se = 0.005 )
# Likelihood ratio test= 226.7  on 20 df,   p=<2e-16
# Wald test            = 427.2  on 20 df,   p=<2e-16
# Score (logrank) test = 241.2  on 20 df,   p=<2e-16,   Robust = 343  p=<2e-16
# 
#   (Note: the likelihood ratio and score tests assume independence of
#      observations within a cluster, the Wald and robust score tests do not).

summary(iiw_model_after_ph_wo_ia$iiw.weight)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.336   0.616   1.124   1.368   1.966   5.427 
Code
#   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
# 0.5302  0.9069  1.1501  1.3870  1.8680  3.0902 




iiw_model_after_ph_wo_ia_alt<-
iiw.weights(Surv(time.lag,time,event)~ 
            cluster(id)+ 
            tr_outcome_rec.lag +
            log_dias_treat_imp_sin_na.lag +
            less_90d_tr1_rec.lag+
            comp_bpsc_y3_severe_rec.lag + 
            policonsumo2.lag + 
            edad_al_ing_1 + 
            ano_nac_corr + 
            susinidum_coc_rec2 +
            susinidum_oh +
            susinidum_pbc +
            susinidum_mar +
            psycom_dum_with_rec2 +
            psycom_dum_study + 
            freq_cons_dum_5day +
            cond_oc_dum_2inact +
            cond_oc_dum_3unemp +
            susprindum_oh +
            susprindum_coc +
            susprindum_pbc +
            susprindum_mar, 
            data= subset(data_mine_miss_restr_proc2, tipo_de_plan_2_mod== levels_tipo_de_plan_2_mod[[4]]) %>% as.data.frame(),
            id= "id",
            time= "time",
            event= "event", #character string indicating which column of the data indicates whether or not a visit occurred. If every row corresponds to a visit, then this column will consist entirely of ones
            maxfu= subset(maxfu_restr_df, subset=  maxfu.id %in% subset(data_mine_miss_restr_proc2, tipo_de_plan_2_mod==levels_tipo_de_plan_2_mod[[4]])$id) %>% as.data.frame(),
            # formulanull = Surv(time.lag,time,event)~ edad_al_ing_1 + 
            #       ano_nac_corr,
            invariant= c("edad_al_ing_1", "ano_nac_corr", "susinidum_coc_rec2", "susinidum_oh", "susinidum_pbc", "susinidum_mar",  "psycom_dum_with_rec2", "psycom_dum_study", "freq_cons_dum_5day", "cond_oc_dum_2inact", "cond_oc_dum_3unemp", "susprindum_oh", "susprindum_coc", "susprindum_pbc", "susprindum_mar"),
            lagvars= c("time", "log_dias_treat_imp_sin_na","tr_outcome_rec", "comp_bpsc_y3_severe_rec", "less_90d_tr1_rec", "policonsumo2"),
            lagfirst= c(2.95082,4.499811,1,1,1,1),  #90/30.5 4.499811 es 90 días
            first= T
)

summary(iiw_model_after_ph_wo_ia_alt$m)
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome_rec.lag + 
    log_dias_treat_imp_sin_na.lag + less_90d_tr1_rec.lag + comp_bpsc_y3_severe_rec.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_coc_rec2 + 
    susinidum_oh + susinidum_pbc + susinidum_mar + psycom_dum_with_rec2 + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 2342, number of events= 1597 
   (163 observations deleted due to missingness)

                                  coef exp(coef) se(coef) robust se      z
tr_outcome_rec.lag             2.07668   7.97793  0.18951   0.17050 12.180
log_dias_treat_imp_sin_na.lag  0.48119   1.61800  0.04878   0.05862  8.208
less_90d_tr1_rec.lag           0.53253   1.70324  0.15975   0.12591  4.230
comp_bpsc_y3_severe_rec.lag    0.24739   1.28067  0.13099   0.12212  2.026
policonsumo2.lag              -0.06660   0.93557  0.07911   0.07156 -0.931
edad_al_ing_1                  0.10711   1.11305  0.01271   0.01175  9.116
ano_nac_corr                   0.11118   1.11759  0.01230   0.01146  9.705
susinidum_coc_rec2            -0.19912   0.81945  0.16603   0.16462 -1.210
susinidum_oh                  -0.24978   0.77897  0.14191   0.12065 -2.070
susinidum_pbc                 -0.07015   0.93225  0.15381   0.13115 -0.535
susinidum_mar                 -0.14500   0.86502  0.14235   0.11908 -1.218
psycom_dum_with_rec2          -0.03390   0.96667  0.05027   0.04078 -0.831
psycom_dum_study              -0.22053   0.80210  0.08298   0.08202 -2.689
freq_cons_dum_5day             0.03082   1.03130  0.05150   0.04617  0.668
cond_oc_dum_2inact             0.02049   1.02070  0.06522   0.06013  0.341
cond_oc_dum_3unemp            -0.01038   0.98968  0.06734   0.06267 -0.166
susprindum_oh                  0.03448   1.03508  0.18281   0.17658  0.195
susprindum_coc                 0.06938   1.07184  0.18629   0.18800  0.369
susprindum_pbc                 0.11002   1.11631  0.18066   0.18587  0.592
susprindum_mar                 0.05398   1.05546  0.19901   0.20360  0.265
                              Pr(>|z|)    
tr_outcome_rec.lag             < 2e-16 ***
log_dias_treat_imp_sin_na.lag 2.24e-16 ***
less_90d_tr1_rec.lag          2.34e-05 ***
comp_bpsc_y3_severe_rec.lag    0.04280 *  
policonsumo2.lag               0.35204    
edad_al_ing_1                  < 2e-16 ***
ano_nac_corr                   < 2e-16 ***
susinidum_coc_rec2             0.22642    
susinidum_oh                   0.03842 *  
susinidum_pbc                  0.59273    
susinidum_mar                  0.22334    
psycom_dum_with_rec2           0.40585    
psycom_dum_study               0.00717 ** 
freq_cons_dum_5day             0.50442    
cond_oc_dum_2inact             0.73326    
cond_oc_dum_3unemp             0.86850    
susprindum_oh                  0.84517    
susprindum_coc                 0.71210    
susprindum_pbc                 0.55390    
susprindum_mar                 0.79092    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome_rec.lag               7.9779     0.1253    5.7117   11.1434
log_dias_treat_imp_sin_na.lag    1.6180     0.6180    1.4424    1.8150
less_90d_tr1_rec.lag             1.7032     0.5871    1.3308    2.1799
comp_bpsc_y3_severe_rec.lag      1.2807     0.7808    1.0081    1.6270
policonsumo2.lag                 0.9356     1.0689    0.8131    1.0764
edad_al_ing_1                    1.1131     0.8984    1.0877    1.1390
ano_nac_corr                     1.1176     0.8948    1.0928    1.1430
susinidum_coc_rec2               0.8194     1.2203    0.5935    1.1315
susinidum_oh                     0.7790     1.2837    0.6149    0.9868
susinidum_pbc                    0.9323     1.0727    0.7209    1.2055
susinidum_mar                    0.8650     1.1560    0.6850    1.0924
psycom_dum_with_rec2             0.9667     1.0345    0.8924    1.0471
psycom_dum_study                 0.8021     1.2467    0.6830    0.9420
freq_cons_dum_5day               1.0313     0.9696    0.9421    1.1290
cond_oc_dum_2inact               1.0207     0.9797    0.9072    1.1484
cond_oc_dum_3unemp               0.9897     1.0104    0.8753    1.1190
susprindum_oh                    1.0351     0.9661    0.7323    1.4631
susprindum_coc                   1.0718     0.9330    0.7415    1.5494
susprindum_pbc                   1.1163     0.8958    0.7755    1.6069
susprindum_mar                   1.0555     0.9475    0.7082    1.5731

Concordance= 0.697  (se = 0.006 )
Likelihood ratio test= 817.7  on 20 df,   p=<2e-16
Wald test            = 852.1  on 20 df,   p=<2e-16
Score (logrank) test = 848  on 20 df,   p=<2e-16,   Robust = 632.9  p=<2e-16

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
Code
# Call:
# coxph(formula = Surv(time.lag, time, event) ~ tr_outcome_rec.lag + 
#     log_dias_treat_imp_sin_na.lag + less_90d_tr1_rec.lag + comp_bpsc_y3_severe_rec.lag + 
#     policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_coc_rec2 + 
#     susinidum_oh + susinidum_pbc + susinidum_mar + psycom_dum_with_rec2 + 
#     psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
#     cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
#     susprindum_mar, data = datacox, cluster = id)
# 
#   n= 2342, number of events= 1597 
#    (163 observations deleted due to missingness)
# 
#                                   coef exp(coef) se(coef) robust se      z Pr(>|z|)    
# tr_outcome_rec.lag             2.07668   7.97793  0.18951   0.17050 12.180  < 2e-16 ***
# log_dias_treat_imp_sin_na.lag  0.48119   1.61800  0.04878   0.05862  8.208 2.24e-16 ***
# less_90d_tr1_rec.lag           0.53253   1.70324  0.15975   0.12591  4.230 2.34e-05 ***
# comp_bpsc_y3_severe_rec.lag    0.24739   1.28067  0.13099   0.12212  2.026  0.04280 *  
# policonsumo2.lag              -0.06660   0.93557  0.07911   0.07156 -0.931  0.35204    
# edad_al_ing_1                  0.10711   1.11305  0.01271   0.01175  9.116  < 2e-16 ***
# ano_nac_corr                   0.11118   1.11759  0.01230   0.01146  9.705  < 2e-16 ***
# susinidum_coc_rec2            -0.19912   0.81945  0.16603   0.16462 -1.210  0.22642    
# susinidum_oh                  -0.24978   0.77897  0.14191   0.12065 -2.070  0.03842 *  
# susinidum_pbc                 -0.07015   0.93225  0.15381   0.13115 -0.535  0.59273    
# susinidum_mar                 -0.14500   0.86502  0.14235   0.11908 -1.218  0.22334    
# psycom_dum_with_rec2          -0.03390   0.96667  0.05027   0.04078 -0.831  0.40585    
# psycom_dum_study              -0.22053   0.80210  0.08298   0.08202 -2.689  0.00717 ** 
# freq_cons_dum_5day             0.03082   1.03130  0.05150   0.04617  0.668  0.50442    
# cond_oc_dum_2inact             0.02049   1.02070  0.06522   0.06013  0.341  0.73326    
# cond_oc_dum_3unemp            -0.01038   0.98968  0.06734   0.06267 -0.166  0.86850    
# susprindum_oh                  0.03448   1.03508  0.18281   0.17658  0.195  0.84517    
# susprindum_coc                 0.06938   1.07184  0.18629   0.18800  0.369  0.71210    
# susprindum_pbc                 0.11002   1.11631  0.18066   0.18587  0.592  0.55390    
# susprindum_mar                 0.05398   1.05546  0.19901   0.20360  0.265  0.79092    
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# 
#                               exp(coef) exp(-coef) lower .95 upper .95
# tr_outcome_rec.lag               7.9779     0.1253    5.7117   11.1434
# log_dias_treat_imp_sin_na.lag    1.6180     0.6180    1.4424    1.8150
# less_90d_tr1_rec.lag             1.7032     0.5871    1.3308    2.1799
# comp_bpsc_y3_severe_rec.lag      1.2807     0.7808    1.0081    1.6270
# policonsumo2.lag                 0.9356     1.0689    0.8131    1.0764
# edad_al_ing_1                    1.1131     0.8984    1.0877    1.1390
# ano_nac_corr                     1.1176     0.8948    1.0928    1.1430
# susinidum_coc_rec2               0.8194     1.2203    0.5935    1.1315
# susinidum_oh                     0.7790     1.2837    0.6149    0.9868
# susinidum_pbc                    0.9323     1.0727    0.7209    1.2055
# susinidum_mar                    0.8650     1.1560    0.6850    1.0924
# psycom_dum_with_rec2             0.9667     1.0345    0.8924    1.0471
# psycom_dum_study                 0.8021     1.2467    0.6830    0.9420
# freq_cons_dum_5day               1.0313     0.9696    0.9421    1.1290
# cond_oc_dum_2inact               1.0207     0.9797    0.9072    1.1484
# cond_oc_dum_3unemp               0.9897     1.0104    0.8753    1.1190
# susprindum_oh                    1.0351     0.9661    0.7323    1.4631
# susprindum_coc                   1.0718     0.9330    0.7415    1.5494
# susprindum_pbc                   1.1163     0.8958    0.7755    1.6069
# susprindum_mar                   1.0555     0.9475    0.7082    1.5731
# 
# Concordance= 0.697  (se = 0.006 )
# Likelihood ratio test= 817.7  on 20 df,   p=<2e-16
# Wald test            = 852.1  on 20 df,   p=<2e-16
# Score (logrank) test = 848  on 20 df,   p=<2e-16,   Robust = 632.9  p=<2e-16
# 
#   (Note: the likelihood ratio and score tests assume independence of
#      observations within a cluster, the Wald and robust score tests do not).

summary(iiw_model_after_ph_wo_ia_alt$iiw.weight)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 0.1065  0.2508  0.7104  1.3025  1.8834 27.4746 
Code
 #   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 # 0.1065  0.2508  0.7104  1.3025  1.8834 27.4746 


#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
invisible("WO residential")

iiw_model_after_ph_wo_res<-
iiw.weights(Surv(time.lag,time,event)~ 
            cluster(id)+ 
            tr_outcome_rec.lag +
            log_dias_treat_imp_sin_na.lag +
            less_90d_tr1_rec.lag+
            comp_bpsc_y3_severe_rec.lag + 
            policonsumo2.lag + 
            edad_al_ing_1 + 
            ano_nac_corr + 
            susinidum_coc_rec2 +
            susinidum_oh +
            susinidum_pbc +
            susinidum_mar +
            psycom_dum_with_rec2 +
            psycom_dum_study + 
            freq_cons_dum_5day +
            cond_oc_dum_2inact +
            cond_oc_dum_3unemp +
            susprindum_oh +
            susprindum_coc +
            susprindum_pbc +
            susprindum_mar, 
            data= subset(data_mine_miss_restr_proc2, tipo_de_plan_2_mod== levels_tipo_de_plan_2_mod[[5]]) %>% as.data.frame(),
            id= "id",
            time= "time",
            event= "event", #character string indicating which column of the data indicates whether or not a visit occurred. If every row corresponds to a visit, then this column will consist entirely of ones
            maxfu= subset(maxfu_restr_df, subset=  maxfu.id %in% subset(data_mine_miss_restr_proc2, tipo_de_plan_2_mod==levels_tipo_de_plan_2_mod[[5]])$id) %>% as.data.frame(),
            # formulanull = Surv(time.lag,time,event)~ edad_al_ing_1 + 
            #       ano_nac_corr,
            invariant= c("edad_al_ing_1", "ano_nac_corr", "susinidum_coc_rec2", "susinidum_oh", "susinidum_pbc", "susinidum_mar",  "psycom_dum_with_rec2", "psycom_dum_study", "freq_cons_dum_5day", "cond_oc_dum_2inact", "cond_oc_dum_3unemp", "susprindum_oh", "susprindum_coc", "susprindum_pbc", "susprindum_mar"),
            lagvars= c("time", "log_dias_treat_imp_sin_na","tr_outcome_rec", "comp_bpsc_y3_severe_rec", "less_90d_tr1_rec", "policonsumo2"),
            lagfirst= c(2.95082,4.499811/2,0,0,0,0),  #90/30.5 4.499811 es 90 días
            first= T
)

summary(iiw_model_after_ph_wo_res$m)
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome_rec.lag + 
    log_dias_treat_imp_sin_na.lag + less_90d_tr1_rec.lag + comp_bpsc_y3_severe_rec.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_coc_rec2 + 
    susinidum_oh + susinidum_pbc + susinidum_mar + psycom_dum_with_rec2 + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 3189, number of events= 2153 
   (381 observations deleted due to missingness)

                                   coef exp(coef)  se(coef) robust se       z
tr_outcome_rec.lag            -0.121612  0.885492  0.165763  0.129366  -0.940
log_dias_treat_imp_sin_na.lag -0.130201  0.877919  0.014462  0.014401  -9.041
less_90d_tr1_rec.lag           0.090970  1.095236  0.149749  0.121156   0.751
comp_bpsc_y3_severe_rec.lag    0.404439  1.498462  0.142265  0.111507   3.627
policonsumo2.lag              -0.669071  0.512184  0.052846  0.047637 -14.045
edad_al_ing_1                  0.033148  1.033703  0.010263  0.006975   4.752
ano_nac_corr                   0.039915  1.040723  0.009991  0.006723   5.937
susinidum_coc_rec2             0.423752  1.527683  0.156368  0.113984   3.718
susinidum_oh                   0.340455  1.405586  0.131706  0.085631   3.976
susinidum_pbc                  0.248609  1.282241  0.138130  0.094813   2.622
susinidum_mar                  0.339920  1.404836  0.132224  0.085995   3.953
psycom_dum_with_rec2           0.080161  1.083462  0.047830  0.034178   2.345
psycom_dum_study              -0.359226  0.698216  0.069917  0.058469  -6.144
freq_cons_dum_5day            -0.014932  0.985179  0.050738  0.034074  -0.438
cond_oc_dum_2inact             0.017569  1.017725  0.081296  0.053721   0.327
cond_oc_dum_3unemp             0.089596  1.093733  0.081392  0.054694   1.638
susprindum_oh                 -0.081599  0.921642  0.147882  0.078177  -1.044
susprindum_coc                 0.046968  1.048088  0.149001  0.085066   0.552
susprindum_pbc                -0.045903  0.955134  0.139802  0.074248  -0.618
susprindum_mar                 0.120782  1.128378  0.210478  0.124728   0.968
                              Pr(>|z|)    
tr_outcome_rec.lag            0.347187    
log_dias_treat_imp_sin_na.lag  < 2e-16 ***
less_90d_tr1_rec.lag          0.452744    
comp_bpsc_y3_severe_rec.lag   0.000287 ***
policonsumo2.lag               < 2e-16 ***
edad_al_ing_1                 2.01e-06 ***
ano_nac_corr                  2.89e-09 ***
susinidum_coc_rec2            0.000201 ***
susinidum_oh                  7.01e-05 ***
susinidum_pbc                 0.008739 ** 
susinidum_mar                 7.72e-05 ***
psycom_dum_with_rec2          0.019005 *  
psycom_dum_study              8.05e-10 ***
freq_cons_dum_5day            0.661225    
cond_oc_dum_2inact            0.743632    
cond_oc_dum_3unemp            0.101394    
susprindum_oh                 0.296592    
susprindum_coc                0.580858    
susprindum_pbc                0.536413    
susprindum_mar                0.332866    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome_rec.lag               0.8855     1.1293    0.6872    1.1410
log_dias_treat_imp_sin_na.lag    0.8779     1.1391    0.8535    0.9031
less_90d_tr1_rec.lag             1.0952     0.9130    0.8637    1.3888
comp_bpsc_y3_severe_rec.lag      1.4985     0.6674    1.2043    1.8645
policonsumo2.lag                 0.5122     1.9524    0.4665    0.5623
edad_al_ing_1                    1.0337     0.9674    1.0197    1.0479
ano_nac_corr                     1.0407     0.9609    1.0271    1.0545
susinidum_coc_rec2               1.5277     0.6546    1.2218    1.9101
susinidum_oh                     1.4056     0.7114    1.1884    1.6624
susinidum_pbc                    1.2822     0.7799    1.0648    1.5441
susinidum_mar                    1.4048     0.7118    1.1869    1.6627
psycom_dum_with_rec2             1.0835     0.9230    1.0133    1.1585
psycom_dum_study                 0.6982     1.4322    0.6226    0.7830
freq_cons_dum_5day               0.9852     1.0150    0.9215    1.0532
cond_oc_dum_2inact               1.0177     0.9826    0.9160    1.1307
cond_oc_dum_3unemp               1.0937     0.9143    0.9826    1.2175
susprindum_oh                    0.9216     1.0850    0.7907    1.0743
susprindum_coc                   1.0481     0.9541    0.8871    1.2382
susprindum_pbc                   0.9551     1.0470    0.8258    1.1048
susprindum_mar                   1.1284     0.8862    0.8837    1.4409

Concordance= 0.612  (se = 0.005 )
Likelihood ratio test= 365.4  on 20 df,   p=<2e-16
Wald test            = 543  on 20 df,   p=<2e-16
Score (logrank) test = 379.9  on 20 df,   p=<2e-16,   Robust = 480.8  p=<2e-16

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
Code
# Call:
# coxph(formula = Surv(time.lag, time, event) ~ tr_outcome_rec.lag + 
#     log_dias_treat_imp_sin_na.lag + less_90d_tr1_rec.lag + comp_bpsc_y3_severe_rec.lag + 
#     policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_coc_rec2 + 
#     susinidum_oh + susinidum_pbc + susinidum_mar + psycom_dum_with_rec2 + 
#     psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
#     cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
#     susprindum_mar, data = datacox, cluster = id)
# 
#   n= 3189, number of events= 2153 
#    (381 observations deleted due to missingness)
# 
#                                    coef exp(coef)  se(coef) robust se       z Pr(>|z|)    
# tr_outcome_rec.lag            -0.126675  0.881020  0.163284  0.124402  -1.018 0.308550    
# log_dias_treat_imp_sin_na.lag  0.031652  1.032159  0.022850  0.021963   1.441 0.149546    
# less_90d_tr1_rec.lag           0.363190  1.437908  0.157358  0.121227   2.996 0.002736 ** 
# comp_bpsc_y3_severe_rec.lag    0.349023  1.417682  0.139042  0.107539   3.246 0.001172 ** 
# policonsumo2.lag              -0.778676  0.459014  0.050648  0.044979 -17.312  < 2e-16 ***
# edad_al_ing_1                  0.023880  1.024168  0.010199  0.006895   3.463 0.000534 ***
# ano_nac_corr                   0.030909  1.031392  0.009928  0.006625   4.666 3.07e-06 ***
# susinidum_coc_rec2             0.353985  1.424734  0.156986  0.101414   3.490 0.000482 ***
# susinidum_oh                   0.291847  1.338899  0.132369  0.078022   3.741 0.000184 ***
# susinidum_pbc                  0.186272  1.204750  0.138703  0.088234   2.111 0.034763 *  
# susinidum_mar                  0.294218  1.342076  0.132819  0.078394   3.753 0.000175 ***
# psycom_dum_with_rec2           0.075649  1.078584  0.048007  0.033882   2.233 0.025566 *  
# psycom_dum_study              -0.341685  0.710572  0.070190  0.057146  -5.979 2.24e-09 ***
# freq_cons_dum_5day            -0.020624  0.979587  0.050671  0.033925  -0.608 0.543239    
# cond_oc_dum_2inact             0.011996  1.012068  0.081341  0.053444   0.224 0.822402    
# cond_oc_dum_3unemp             0.076550  1.079556  0.081384  0.054527   1.404 0.160354    
# susprindum_oh                 -0.077120  0.925778  0.148321  0.077766  -0.992 0.321346    
# susprindum_coc                 0.072353  1.075035  0.149194  0.083542   0.866 0.386453    
# susprindum_pbc                -0.011860  0.988210  0.140132  0.073586  -0.161 0.871956    
# susprindum_mar                 0.154065  1.166566  0.210712  0.124652   1.236 0.216472    
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# 
#                               exp(coef) exp(-coef) lower .95 upper .95
# tr_outcome_rec.lag               0.8810     1.1350    0.6904    1.1243
# log_dias_treat_imp_sin_na.lag    1.0322     0.9688    0.9887    1.0776
# less_90d_tr1_rec.lag             1.4379     0.6955    1.1338    1.8236
# comp_bpsc_y3_severe_rec.lag      1.4177     0.7054    1.1483    1.7503
# policonsumo2.lag                 0.4590     2.1786    0.4203    0.5013
# edad_al_ing_1                    1.0242     0.9764    1.0104    1.0381
# ano_nac_corr                     1.0314     0.9696    1.0181    1.0449
# susinidum_coc_rec2               1.4247     0.7019    1.1679    1.7380
# susinidum_oh                     1.3389     0.7469    1.1490    1.5601
# susinidum_pbc                    1.2047     0.8300    1.0134    1.4322
# susinidum_mar                    1.3421     0.7451    1.1509    1.5650
# psycom_dum_with_rec2             1.0786     0.9271    1.0093    1.1526
# psycom_dum_study                 0.7106     1.4073    0.6353    0.7948
# freq_cons_dum_5day               0.9796     1.0208    0.9166    1.0469
# cond_oc_dum_2inact               1.0121     0.9881    0.9114    1.1238
# cond_oc_dum_3unemp               1.0796     0.9263    0.9701    1.2013
# susprindum_oh                    0.9258     1.0802    0.7949    1.0782
# susprindum_coc                   1.0750     0.9302    0.9127    1.2663
# susprindum_pbc                   0.9882     1.0119    0.8555    1.1415
# susprindum_mar                   1.1666     0.8572    0.9137    1.4894
# 
# Concordance= 0.612  (se = 0.005 )
# Likelihood ratio test= 298.2  on 20 df,   p=<2e-16
# Wald test            = 480.2  on 20 df,   p=<2e-16
# Score (logrank) test = 312.9  on 20 df,   p=<2e-16,   Robust = 428.9  p=<2e-16
# 
#   (Note: the likelihood ratio and score tests assume independence of
#      observations within a cluster, the Wald and robust score tests do not).

summary(iiw_model_after_ph_wo_res$iiw.weight)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 0.2809  0.6732  1.0225  1.1916  1.6473  3.6104 
Code
 #   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 # 0.4578  0.8652  1.2333  1.3288  1.6967  4.6168 




iiw_model_after_ph_wo_res_alt<-
iiw.weights(Surv(time.lag,time,event)~ 
            cluster(id)+ 
            tr_outcome_rec.lag +
            log_dias_treat_imp_sin_na.lag +
            less_90d_tr1_rec.lag+
            comp_bpsc_y3_severe_rec.lag + 
            policonsumo2.lag + 
            edad_al_ing_1 + 
            ano_nac_corr + 
            susinidum_coc_rec2 +
            susinidum_oh +
            susinidum_pbc +
            susinidum_mar +
            psycom_dum_with_rec2 +
            psycom_dum_study + 
            freq_cons_dum_5day +
            cond_oc_dum_2inact +
            cond_oc_dum_3unemp +
            susprindum_oh +
            susprindum_coc +
            susprindum_pbc +
            susprindum_mar, 
            data= subset(data_mine_miss_restr_proc2, tipo_de_plan_2_mod== levels_tipo_de_plan_2_mod[[5]]) %>% as.data.frame(),
            id= "id",
            time= "time",
            event= "event", #character string indicating which column of the data indicates whether or not a visit occurred. If every row corresponds to a visit, then this column will consist entirely of ones
            maxfu= subset(maxfu_restr_df, subset=  maxfu.id %in% subset(data_mine_miss_restr_proc2, tipo_de_plan_2_mod==levels_tipo_de_plan_2_mod[[5]])$id) %>% as.data.frame(),
            # formulanull = Surv(time.lag,time,event)~ edad_al_ing_1 + 
            #       ano_nac_corr,
            invariant= c("edad_al_ing_1", "ano_nac_corr", "susinidum_coc_rec2", "susinidum_oh", "susinidum_pbc", "susinidum_mar",  "psycom_dum_with_rec2", "psycom_dum_study", "freq_cons_dum_5day", "cond_oc_dum_2inact", "cond_oc_dum_3unemp", "susprindum_oh", "susprindum_coc", "susprindum_pbc", "susprindum_mar"),
            lagvars= c("time", "log_dias_treat_imp_sin_na","tr_outcome_rec", "comp_bpsc_y3_severe_rec", "less_90d_tr1_rec", "policonsumo2"),
            lagfirst= c(2.95082,4.499811,1,1,1,1),  #90/30.5 4.499811 es 90 días
            first= T
)

summary(iiw_model_after_ph_wo_res_alt$m)
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome_rec.lag + 
    log_dias_treat_imp_sin_na.lag + less_90d_tr1_rec.lag + comp_bpsc_y3_severe_rec.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_coc_rec2 + 
    susinidum_oh + susinidum_pbc + susinidum_mar + psycom_dum_with_rec2 + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 3189, number of events= 2153 
   (381 observations deleted due to missingness)

                                    coef  exp(coef)   se(coef)  robust se
tr_outcome_rec.lag             0.7205801  2.0556254  0.1852413  0.1688667
log_dias_treat_imp_sin_na.lag  0.2567171  1.2926793  0.0273630  0.0340385
less_90d_tr1_rec.lag           0.6130288  1.8460141  0.1493355  0.1272692
comp_bpsc_y3_severe_rec.lag    0.8473593  2.3334766  0.1535169  0.1444312
policonsumo2.lag               0.0184737  1.0186454  0.0734914  0.0667181
edad_al_ing_1                  0.0575836  1.0592739  0.0104369  0.0089922
ano_nac_corr                   0.0650981  1.0672637  0.0101748  0.0087168
susinidum_coc_rec2             0.3116140  1.3656274  0.1602666  0.1367453
susinidum_oh                   0.1895267  1.2086774  0.1337491  0.0914487
susinidum_pbc                  0.1983614  1.2194030  0.1404151  0.1021221
susinidum_mar                  0.1989101  1.2200723  0.1343948  0.0922898
psycom_dum_with_rec2          -0.0087068  0.9913310  0.0483988  0.0404580
psycom_dum_study              -0.3746894  0.6875028  0.0680287  0.0652967
freq_cons_dum_5day             0.0004056  1.0004056  0.0506595  0.0444514
cond_oc_dum_2inact             0.0813419  1.0847417  0.0809746  0.0759279
cond_oc_dum_3unemp             0.1300692  1.1389072  0.0814469  0.0765984
susprindum_oh                 -0.1079311  0.8976894  0.1483977  0.1155415
susprindum_coc                 0.0027078  1.0027114  0.1498413  0.1207600
susprindum_pbc                -0.0637471  0.9382423  0.1407408  0.1099733
susprindum_mar                -0.0838286  0.9195888  0.2112160  0.1592995
                                   z Pr(>|z|)    
tr_outcome_rec.lag             4.267 1.98e-05 ***
log_dias_treat_imp_sin_na.lag  7.542 4.63e-14 ***
less_90d_tr1_rec.lag           4.817 1.46e-06 ***
comp_bpsc_y3_severe_rec.lag    5.867 4.44e-09 ***
policonsumo2.lag               0.277   0.7819    
edad_al_ing_1                  6.404 1.52e-10 ***
ano_nac_corr                   7.468 8.14e-14 ***
susinidum_coc_rec2             2.279   0.0227 *  
susinidum_oh                   2.072   0.0382 *  
susinidum_pbc                  1.942   0.0521 .  
susinidum_mar                  2.155   0.0311 *  
psycom_dum_with_rec2          -0.215   0.8296    
psycom_dum_study              -5.738 9.57e-09 ***
freq_cons_dum_5day             0.009   0.9927    
cond_oc_dum_2inact             1.071   0.2840    
cond_oc_dum_3unemp             1.698   0.0895 .  
susprindum_oh                 -0.934   0.3502    
susprindum_coc                 0.022   0.9821    
susprindum_pbc                -0.580   0.5621    
susprindum_mar                -0.526   0.5987    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome_rec.lag               2.0556     0.4865    1.4764    2.8621
log_dias_treat_imp_sin_na.lag    1.2927     0.7736    1.2093    1.3819
less_90d_tr1_rec.lag             1.8460     0.5417    1.4385    2.3690
comp_bpsc_y3_severe_rec.lag      2.3335     0.4285    1.7582    3.0970
policonsumo2.lag                 1.0186     0.9817    0.8938    1.1610
edad_al_ing_1                    1.0593     0.9440    1.0408    1.0781
ano_nac_corr                     1.0673     0.9370    1.0492    1.0857
susinidum_coc_rec2               1.3656     0.7323    1.0446    1.7854
susinidum_oh                     1.2087     0.8274    1.0103    1.4459
susinidum_pbc                    1.2194     0.8201    0.9982    1.4896
susinidum_mar                    1.2201     0.8196    1.0182    1.4620
psycom_dum_with_rec2             0.9913     1.0087    0.9158    1.0731
psycom_dum_study                 0.6875     1.4545    0.6049    0.7814
freq_cons_dum_5day               1.0004     0.9996    0.9169    1.0915
cond_oc_dum_2inact               1.0847     0.9219    0.9348    1.2588
cond_oc_dum_3unemp               1.1389     0.8780    0.9801    1.3234
susprindum_oh                    0.8977     1.1140    0.7158    1.1258
susprindum_coc                   1.0027     0.9973    0.7914    1.2705
susprindum_pbc                   0.9382     1.0658    0.7563    1.1639
susprindum_mar                   0.9196     1.0874    0.6730    1.2566

Concordance= 0.666  (se = 0.005 )
Likelihood ratio test= 814.4  on 20 df,   p=<2e-16
Wald test            = 982.1  on 20 df,   p=<2e-16
Score (logrank) test = 834.4  on 20 df,   p=<2e-16,   Robust = 780.6  p=<2e-16

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
Code
# Call:
# coxph(formula = Surv(time.lag, time, event) ~ tr_outcome_rec.lag + 
#     log_dias_treat_imp_sin_na.lag + less_90d_tr1_rec.lag + comp_bpsc_y3_severe_rec.lag + 
#     policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_coc_rec2 + 
#     susinidum_oh + susinidum_pbc + susinidum_mar + psycom_dum_with_rec2 + 
#     psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
#     cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
#     susprindum_mar, data = datacox, cluster = id)
# 
#   n= 3189, number of events= 2153 
#    (381 observations deleted due to missingness)
# 
#                                     coef  exp(coef)   se(coef)  robust se      z Pr(>|z|)    
# tr_outcome_rec.lag             0.7205801  2.0556254  0.1852413  0.1688667  4.267 1.98e-05 ***
# log_dias_treat_imp_sin_na.lag  0.2567171  1.2926793  0.0273630  0.0340385  7.542 4.63e-14 ***
# less_90d_tr1_rec.lag           0.6130288  1.8460141  0.1493355  0.1272692  4.817 1.46e-06 ***
# comp_bpsc_y3_severe_rec.lag    0.8473593  2.3334766  0.1535169  0.1444312  5.867 4.44e-09 ***
# policonsumo2.lag               0.0184737  1.0186454  0.0734914  0.0667181  0.277   0.7819    
# edad_al_ing_1                  0.0575836  1.0592739  0.0104369  0.0089922  6.404 1.52e-10 ***
# ano_nac_corr                   0.0650981  1.0672637  0.0101748  0.0087168  7.468 8.14e-14 ***
# susinidum_coc_rec2             0.3116140  1.3656274  0.1602666  0.1367453  2.279   0.0227 *  
# susinidum_oh                   0.1895267  1.2086774  0.1337491  0.0914487  2.072   0.0382 *  
# susinidum_pbc                  0.1983614  1.2194030  0.1404151  0.1021221  1.942   0.0521 .  
# susinidum_mar                  0.1989101  1.2200723  0.1343948  0.0922898  2.155   0.0311 *  
# psycom_dum_with_rec2          -0.0087068  0.9913310  0.0483988  0.0404580 -0.215   0.8296    
# psycom_dum_study              -0.3746894  0.6875028  0.0680287  0.0652967 -5.738 9.57e-09 ***
# freq_cons_dum_5day             0.0004056  1.0004056  0.0506595  0.0444514  0.009   0.9927    
# cond_oc_dum_2inact             0.0813419  1.0847417  0.0809746  0.0759279  1.071   0.2840    
# cond_oc_dum_3unemp             0.1300692  1.1389072  0.0814469  0.0765984  1.698   0.0895 .  
# susprindum_oh                 -0.1079311  0.8976894  0.1483977  0.1155415 -0.934   0.3502    
# susprindum_coc                 0.0027078  1.0027114  0.1498413  0.1207600  0.022   0.9821    
# susprindum_pbc                -0.0637471  0.9382423  0.1407408  0.1099733 -0.580   0.5621    
# susprindum_mar                -0.0838286  0.9195888  0.2112160  0.1592995 -0.526   0.5987    
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# 
#                               exp(coef) exp(-coef) lower .95 upper .95
# tr_outcome_rec.lag               2.0556     0.4865    1.4764    2.8621
# log_dias_treat_imp_sin_na.lag    1.2927     0.7736    1.2093    1.3819
# less_90d_tr1_rec.lag             1.8460     0.5417    1.4385    2.3690
# comp_bpsc_y3_severe_rec.lag      2.3335     0.4285    1.7582    3.0970
# policonsumo2.lag                 1.0186     0.9817    0.8938    1.1610
# edad_al_ing_1                    1.0593     0.9440    1.0408    1.0781
# ano_nac_corr                     1.0673     0.9370    1.0492    1.0857
# susinidum_coc_rec2               1.3656     0.7323    1.0446    1.7854
# susinidum_oh                     1.2087     0.8274    1.0103    1.4459
# susinidum_pbc                    1.2194     0.8201    0.9982    1.4896
# susinidum_mar                    1.2201     0.8196    1.0182    1.4620
# psycom_dum_with_rec2             0.9913     1.0087    0.9158    1.0731
# psycom_dum_study                 0.6875     1.4545    0.6049    0.7814
# freq_cons_dum_5day               1.0004     0.9996    0.9169    1.0915
# cond_oc_dum_2inact               1.0847     0.9219    0.9348    1.2588
# cond_oc_dum_3unemp               1.1389     0.8780    0.9801    1.3234
# susprindum_oh                    0.8977     1.1140    0.7158    1.1258
# susprindum_coc                   1.0027     0.9973    0.7914    1.2705
# susprindum_pbc                   0.9382     1.0658    0.7563    1.1639
# susprindum_mar                   0.9196     1.0874    0.6730    1.2566
# 
# Concordance= 0.666  (se = 0.005 )
# Likelihood ratio test= 814.4  on 20 df,   p=<2e-16
# Wald test            = 982.1  on 20 df,   p=<2e-16
# Score (logrank) test = 834.4  on 20 df,   p=<2e-16,   Robust = 780.6  p=<2e-16
# 
#   (Note: the likelihood ratio and score tests assume independence of
#      observations within a cluster, the Wald and robust score tests do not).

summary(iiw_model_after_ph_wo_res_alt$iiw.weight)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 0.1235  0.2374  0.5581  0.8884  1.2695 52.2809 
Code
#   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
# 0.1235  0.2374  0.5581  0.8884  1.2695 52.2809 
Code
data_mine_miss_restr_proc2_iiw_after_ph <- NULL # Initialize empty result

df_models_after_ph<-
cbind.data.frame(tipo_de_plan_2= attr(table(data_mine_miss_restr_proc2$tipo_de_plan_2_mod),"names"), 
                 models_after_ph= c("iiw_model_after_ph_ba", 
                                    "iiw_model_after_ph_gp_ia", 
                                    "iiw_model_after_ph_gp_res", 
                                    "iiw_model_after_ph_wo_ia", 
                                    "iiw_model_after_ph_wo_res"))

  for (i in 1:nrow(df_models_after_ph)) {
    subset_data0 <- subset(dplyr::mutate(data_mine_miss_restr_proc2, rn=dplyr::row_number()), subset= tipo_de_plan_2_mod== df_models_after_ph[i,1])
    
    subset_data02<- cbind.data.frame(subset_data0, iiw_after_ph= get(df_models_after_ph[i,2])$iiw.weight)
    
    data_mine_miss_restr_proc2_iiw_after_ph <- rbind.data.frame(data_mine_miss_restr_proc2_iiw_after_ph, subset_data02)
  }

#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_

data_mine_miss_restr_proc2_iiw_after_ph_alt <- NULL # Initialize empty result

df_models_after_ph_alt<-
cbind.data.frame(tipo_de_plan_2= attr(table(data_mine_miss_restr_proc2$tipo_de_plan_2_mod),"names"), 
                 models_after_ph= paste0(c("iiw_model_after_ph_ba", 
                                    "iiw_model_after_ph_gp_ia", 
                                    "iiw_model_after_ph_gp_res", 
                                    "iiw_model_after_ph_wo_ia", 
                                    "iiw_model_after_ph_wo_res"),"_alt"))
  for (i in 1:nrow(df_models_after_ph_alt)) {
    subset_data00 <- subset(dplyr::mutate(data_mine_miss_restr_proc2, rn=dplyr::row_number()), subset= tipo_de_plan_2_mod== df_models_after_ph_alt[i,1])
    
    subset_data002<- cbind.data.frame(subset_data00, iiw_after_ph= get(df_models_after_ph_alt[i,2])$iiw.weight)
    
    data_mine_miss_restr_proc2_iiw_after_ph_alt <- rbind.data.frame(data_mine_miss_restr_proc2_iiw_after_ph_alt, subset_data002)
  }

summary(data_mine_miss_restr_proc2_iiw_after_ph$iiw_after_ph)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
0.09172 0.66527 1.08442 1.33483 1.90360 5.42701 
Code
 #   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 # 0.4578  0.8791  1.1600  1.3930  1.8724  6.7300 

summary(data_mine_miss_restr_proc2_iiw_after_ph_alt$iiw_after_ph)
     Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
   0.0643    0.1688    0.5455    1.1908    1.3579 1294.5167 
Code
#  Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
# 0.0643    0.1688    0.5455    1.1908    1.3579 1294.5167 

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
invisible("Stabilize weights")
data_mine_miss_restr_proc2_iiw_after_ph$iiw_after_ph_st<-data_mine_miss_restr_proc2_iiw_after_ph$iiw_after_ph
data_mine_miss_restr_proc2_iiw_after_ph$iiw_after_ph_st[data_mine_miss_restr_proc2_iiw_after_ph$iiw_after_ph>quantile(data_mine_miss_restr_proc2_iiw_after_ph$iiw_after_ph,0.975)] <- quantile(data_mine_miss_restr_proc2_iiw_after_ph$iiw_after_ph,0.975)
data_mine_miss_restr_proc2_iiw_after_ph$iiw_after_ph_st[data_mine_miss_restr_proc2_iiw_after_ph$iiw_after_ph<quantile(data_mine_miss_restr_proc2_iiw_after_ph$iiw_after_ph,0.025)] <- quantile(data_mine_miss_restr_proc2_iiw_after_ph$iiw_after_ph,0.025)

invisible("stabilize alternative weights")
data_mine_miss_restr_proc2_iiw_after_ph_alt$iiw_after_ph_st<-data_mine_miss_restr_proc2_iiw_after_ph_alt$iiw_after_ph
data_mine_miss_restr_proc2_iiw_after_ph_alt$iiw_after_ph_st[data_mine_miss_restr_proc2_iiw_after_ph_alt$iiw_after_ph>quantile(data_mine_miss_restr_proc2_iiw_after_ph_alt$iiw_after_ph,0.975)] <- quantile(data_mine_miss_restr_proc2_iiw_after_ph_alt$iiw_after_ph,0.975)
data_mine_miss_restr_proc2_iiw_after_ph_alt$iiw_after_ph_st[data_mine_miss_restr_proc2_iiw_after_ph_alt$iiw_after_ph<quantile(data_mine_miss_restr_proc2_iiw_after_ph_alt$iiw_after_ph,0.025)] <- quantile(data_mine_miss_restr_proc2_iiw_after_ph_alt$iiw_after_ph,0.025)


summary(data_mine_miss_restr_proc2_iiw_after_ph$iiw_after_ph_st)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 0.4682  0.6653  1.0844  1.3266  1.9036  3.0250 
Code
#   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
# 0.6912  0.8791  1.1600  1.3873  1.8724  2.5741 

summary(data_mine_miss_restr_proc2_iiw_after_ph_alt$iiw_after_ph_st)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
0.09766 0.16882 0.54545 0.88019 1.35785 3.56260 
Code
#    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
# 0.09766 0.16882 0.54545 0.88019 1.35785 3.56260 


rbind.data.frame(
cbind.data.frame(model= "Primary (after PH, lag=0)", t(matrix(summary(data_mine_miss_restr_proc2_iiw_after_ph$iiw_after_ph_st)))),
cbind.data.frame(model= "Alternative (after PH, lag=1)", t(matrix(summary(data_mine_miss_restr_proc2_iiw_after_ph_alt$iiw_after_ph_st)))))%>% 
  { 
    write.table(., file = paste0(getwd(),"/_proposal_grant/2023/iiw_after_ph_240426.csv"), dec=",", sep="\t")
    knitr::kable(., size=10, format="markdown", caption="Weights (after correction for PHs)", col.names= c("Weight",attr(summary(data_mine_miss_restr_proc2_iiw_after_ph$iiw_after_ph_st),"names")) ) 
  }
Weights (after correction for PHs)
Weight Min. 1st Qu. Median Mean 3rd Qu. Max.
Primary (after PH, lag=0) 0.4681976 0.6652737 1.084424 1.3266201 1.903597 3.025044
Alternative (after PH, lag=1) 0.0976600 0.1688223 0.545454 0.8801874 1.357854 3.562599
GEE
Code
#The id= option is where we specify the clusters within which we have repeated observations that may be correlated, and tehe scale.fix=T option is to avoid R's default approach of introducing an overdispersion scale parameter.
#However, caution is advised when employing QIC and its use should not be routine, see Wang et al. (2015). growing use of quasi-likelihood-based information criteria for longitudinal data to select a working correlation structure in a generalized estimating equation framework. [https://onlinelibrary.wiley.com/doi/full/10.1002/sta4.95]

#poisson(link = "log"),

#https://www.thelancet.com/cms/10.1016/S2468-2667(22)00042-1/attachment/eb69ffc4-cb6c-4a47-9524-94e1ba9302b2/mmc1.pdf
#https://www.thelancet.com/cms/10.1016/S2468-2667(22)00201-8/attachment/05a17ae1-1e31-4ffd-a727-78ec915dc03b/mmc1.pdf

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

invisible("Must be independence structure")

m <- geeglm( tr_outcome  ~ policonsumo2 +
            edad_al_ing_1 + 
            ano_nac_corr + 
            susinidum_oh +
            susinidum_coc +
            susinidum_pbc +
            susinidum_mar +
            psycom_dum_study +
            psycom_dum_with +
            freq_cons_dum_5day +
            cond_oc_dum_2inact +
            cond_oc_dum_3unemp +
            susprindum_coc +
            susprindum_pbc +
            susprindum_mar, 
id=id, data=data_mine_miss_restr_proc2_iiw_after_ph, family=binomial, corstr="independence")
summary(m)

Call:
geeglm(formula = tr_outcome ~ policonsumo2 + edad_al_ing_1 + 
    ano_nac_corr + susinidum_oh + susinidum_coc + susinidum_pbc + 
    susinidum_mar + psycom_dum_study + psycom_dum_with + freq_cons_dum_5day + 
    cond_oc_dum_2inact + cond_oc_dum_3unemp + susprindum_coc + 
    susprindum_pbc + susprindum_mar, family = binomial, data = data_mine_miss_restr_proc2_iiw_after_ph, 
    id = id, corstr = "independence")

 Coefficients:
                     Estimate    Std.err    Wald Pr(>|W|)    
(Intercept)        -43.338627  13.168777  10.831 0.000998 ***
policonsumo2         0.057600   0.035967   2.565 0.109271    
edad_al_ing_1       -0.006693   0.006676   1.005 0.316079    
ano_nac_corr         0.022507   0.006543  11.831 0.000583 ***
susinidum_oh        -0.073882   0.094343   0.613 0.433556    
susinidum_coc       -0.015069   0.118072   0.016 0.898446    
susinidum_pbc        0.145421   0.107807   1.820 0.177366    
susinidum_mar        0.047494   0.096584   0.242 0.622904    
psycom_dum_study     0.863462   0.046317 347.538  < 2e-16 ***
psycom_dum_with      0.060458   0.032631   3.433 0.063916 .  
freq_cons_dum_5day  -0.029792   0.030889   0.930 0.334802    
cond_oc_dum_2inact  -0.258428   0.042900  36.288 1.70e-09 ***
cond_oc_dum_3unemp  -0.192816   0.034495  31.245 2.27e-08 ***
susprindum_coc       0.029585   0.048404   0.374 0.541054    
susprindum_pbc       0.237070   0.040895  33.606 6.75e-09 ***
susprindum_mar       0.062763   0.074961   0.701 0.402438    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Correlation structure = independence 
Estimated Scale Parameters:

            Estimate Std.err
(Intercept)    1.001 0.01624
Number of clusters:   13317  Maximum cluster size: 10 
Code
m_full <- geeglm( tr_outcome  ~ policonsumo2 +
          comp_bpsc_y2_moderate+ 
          comp_bpsc_y3_severe+ 
          edad_al_ing_1+ 
          ano_nac_corr+ 
          esc_dum_rec_3prim+ 
          esc_dum_rec_2high +
          susprindum_oh +
          susprindum_coc +
          susprindum_pbc +
          susprindum_mar+
          freq_cons_dum_5day+
          freq_cons_dum_44to6wk+
          freq_cons_dum_32to3wk+
          freq_cons_dum_21wkmore+
          cond_oc_dum_3unemp+
          cond_oc_dum_2inact+
          viv_dum_illegal+
          viv_dum_own+
          viv_dum_rent+
          viv_dum_temp+
          psycom_dum_with+
          psycom_dum_study+
          susinidum_oh +
          susinidum_coc +
          susinidum_pbc +
          susinidum_mar+
          cohab_dum_alone+
          cohab_dum_fam_or+
          cohab_dum_cpl_child, 
id=id, data=data_mine_miss_restr_proc2_iiw_after_ph, family=binomial, corstr="independence") 


rbind.data.frame(cbind.data.frame(model="simplest", broom::tidy(m, exponentiate = T, conf.int=T)),cbind.data.frame(model="full", broom::tidy(m_full, exponentiate = T, conf.int=T))) %>% dplyr::mutate_at(c("estimate","std.error","conf.low","conf.high"), ~sprintf("%1.2f",.))%>% dplyr::mutate_at(c("p.value"), ~sprintf("%1.4f",.)) %>% dplyr::select(-statistic) %>% 
  { 
    write.table(., file = paste0(getwd(),"/_proposal_grant/2023/gee_240502.csv"), dec=",", sep="\t")
    knitr::kable(dplyr::filter(.,term=="policonsumo2"), size=10, format="markdown", caption="GEE Models (after correction for PHs)") 
  }
GEE Models (after correction for PHs)
model term estimate std.error p.value conf.low conf.high
simplest policonsumo2 1.06 0.04 0.1093 0.99 1.14
full policonsumo2 1.10 0.04 0.0079 1.03 1.18
Code
data_mine_miss_restr_proc2_iiw_after_ph$edad_al_ing_1_st = data_mine_miss_restr_proc2_iiw_after_ph$edad_al_ing_1 - mean(data_mine_miss_restr_proc2_iiw_after_ph$edad_al_ing_1)

# can use to fit a weighted GEE
mw <- geeglm( tr_outcome  ~ policonsumo2 +
            edad_al_ing_1 + 
            ano_nac_corr + 
            susinidum_oh +
            susinidum_coc +
            susinidum_pbc +
            susinidum_mar +
            psycom_dum_study +
            psycom_dum_with +
            freq_cons_dum_5day +
            cond_oc_dum_2inact +
            cond_oc_dum_3unemp +
            susprindum_coc +
            susprindum_pbc +
            susprindum_mar, 
            id=id, data=data_mine_miss_restr_proc2_iiw_after_ph, family= binomial, weights=iiw_after_ph_st, corstr="independence")

Warning in eval(family$initialize): non-integer #successes in a binomial glm!

Code
#In eval(family$initialize) : non-integer #successes in a binomial glm!
#summary(mw)

mwpois <- geeglm( tr_outcome  ~ policonsumo2 +
            edad_al_ing_1 + 
            ano_nac_corr + 
            susinidum_oh +
            susinidum_coc +
            susinidum_pbc +
            susinidum_mar +
            psycom_dum_study +
            psycom_dum_with +
            freq_cons_dum_5day +
            cond_oc_dum_2inact +
            cond_oc_dum_3unemp +
            susprindum_coc +
            susprindum_pbc +
            susprindum_mar, 
            id=id, data=data_mine_miss_restr_proc2_iiw_after_ph, family= poisson(link = "log"), weights=iiw_after_ph_st, corstr="independence")
summary(mwpois)

Call:
geeglm(formula = tr_outcome ~ policonsumo2 + edad_al_ing_1 + 
    ano_nac_corr + susinidum_oh + susinidum_coc + susinidum_pbc + 
    susinidum_mar + psycom_dum_study + psycom_dum_with + freq_cons_dum_5day + 
    cond_oc_dum_2inact + cond_oc_dum_3unemp + susprindum_coc + 
    susprindum_pbc + susprindum_mar, family = poisson(link = "log"), 
    data = data_mine_miss_restr_proc2_iiw_after_ph, weights = iiw_after_ph_st, 
    id = id, corstr = "independence")

 Coefficients:
                   Estimate  Std.err   Wald Pr(>|W|)    
(Intercept)        -6.48845  3.56281   3.32   0.0686 .  
policonsumo2        0.02903  0.01098   6.99   0.0082 ** 
edad_al_ing_1      -0.00520  0.00182   8.13   0.0044 ** 
ano_nac_corr        0.00318  0.00177   3.23   0.0724 .  
susinidum_oh       -0.01208  0.02868   0.18   0.6736    
susinidum_coc       0.00578  0.03446   0.03   0.8668    
susinidum_pbc       0.03984  0.03091   1.66   0.1975    
susinidum_mar       0.01794  0.02878   0.39   0.5331    
psycom_dum_study    0.13012  0.01024 161.40  < 2e-16 ***
psycom_dum_with     0.00609  0.01002   0.37   0.5436    
freq_cons_dum_5day  0.00429  0.00860   0.25   0.6179    
cond_oc_dum_2inact -0.05450  0.01246  19.14  1.2e-05 ***
cond_oc_dum_3unemp -0.03855  0.00943  16.71  4.4e-05 ***
susprindum_coc      0.01730  0.01487   1.35   0.2447    
susprindum_pbc      0.05929  0.01247  22.60  2.0e-06 ***
susprindum_mar      0.01361  0.02142   0.40   0.5252    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Correlation structure = independence 
Estimated Scale Parameters:

            Estimate Std.err
(Intercept)    0.256 0.00339
Number of clusters:   13317  Maximum cluster size: 10 
Code
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

mw_full <- geeglm( tr_outcome  ~ policonsumo2 +
           comp_bpsc_y2_moderate+ 
          comp_bpsc_y3_severe+ 
          edad_al_ing_1+ 
          ano_nac_corr+ 
          esc_dum_rec_3prim+ 
          esc_dum_rec_2high +
          susprindum_oh +
          susprindum_coc +
          susprindum_pbc +
          susprindum_mar+
          freq_cons_dum_5day+
          freq_cons_dum_44to6wk+
          freq_cons_dum_32to3wk+
          freq_cons_dum_21wkmore+
          cond_oc_dum_3unemp+
          cond_oc_dum_2inact+
          viv_dum_illegal+
          viv_dum_own+
          viv_dum_rent+
          viv_dum_temp+
          psycom_dum_with+
          psycom_dum_study+
          susinidum_oh +
          susinidum_coc +
          susinidum_pbc +
          susinidum_mar+
          cohab_dum_alone+
          cohab_dum_fam_or+
          cohab_dum_cpl_child, 
id=id, data=data_mine_miss_restr_proc2_iiw_after_ph, family=binomial, weights=iiw_after_ph_st, corstr="independence") 

Warning in eval(family$initialize): non-integer #successes in a binomial glm!

Code
#In eval(family$initialize) : non-integer #successes in a binomial glm!


mwpois_full <- geeglm( tr_outcome  ~ policonsumo2 +
           comp_bpsc_y2_moderate+ 
          comp_bpsc_y3_severe+ 
          edad_al_ing_1+ 
          ano_nac_corr+ 
          esc_dum_rec_3prim+ 
          esc_dum_rec_2high +
          susprindum_oh +
          susprindum_coc +
          susprindum_pbc +
          susprindum_mar+
          freq_cons_dum_5day+
          freq_cons_dum_44to6wk+
          freq_cons_dum_32to3wk+
          freq_cons_dum_21wkmore+
          cond_oc_dum_3unemp+
          cond_oc_dum_2inact+
          viv_dum_illegal+
          viv_dum_own+
          viv_dum_rent+
          viv_dum_temp+
          psycom_dum_with+
          psycom_dum_study+
          susinidum_oh +
          susinidum_coc +
          susinidum_pbc +
          susinidum_mar+
          cohab_dum_alone+
          cohab_dum_fam_or+
          cohab_dum_cpl_child, 
id=id, data=data_mine_miss_restr_proc2_iiw_after_ph, family= poisson(link = "log"), weights=iiw_after_ph_st, corstr="independence") 

invisible("Model fit")
#broom::glance(mwpois_full)
#QIC(mw)


#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

rbind.data.frame(cbind.data.frame(model="simplest, binomial", broom::tidy(mw, exponentiate = T, conf.int=T)),cbind.data.frame(model="full, binomial", broom::tidy(mw_full, exponentiate = T, conf.int=T)),cbind.data.frame(model="simplest, poisson", broom::tidy(mwpois, exponentiate = T, conf.int=T)),cbind.data.frame(model="full, poisson", broom::tidy(mwpois_full, exponentiate = T, conf.int=T))) %>% dplyr::mutate_at(c("estimate","std.error","conf.low","conf.high"), ~sprintf("%1.2f",.))%>% dplyr::mutate_at(c("p.value"), ~sprintf("%1.4f",.)) %>% dplyr::select(-statistic) %>% 
  { 
    write.table(., file = paste0(getwd(),"/_proposal_grant/2023/geew_240502.csv"), dec=",", sep="\t")
    knitr::kable(dplyr::filter(.,term=="policonsumo2"), size=10, format="markdown", caption="GEE Models, weighted (after correction for PHs)") 
  }
GEE Models, weighted (after correction for PHs)
model term estimate std.error p.value conf.low conf.high
simplest, binomial policonsumo2 1.10 0.04 0.0238 1.01 1.19
full, binomial policonsumo2 1.13 0.04 0.0032 1.04 1.22
simplest, poisson policonsumo2 1.03 0.01 0.0082 1.01 1.05
full, poisson policonsumo2 1.04 0.01 0.0013 1.01 1.06
Code
# can use to fit a weighted GEE
mw_alt <- geeglm( tr_outcome  ~ policonsumo2 +
            edad_al_ing_1 + 
            ano_nac_corr + 
            susinidum_oh +
            susinidum_coc +
            susinidum_pbc +
            susinidum_mar +
            psycom_dum_study +
            psycom_dum_with +
            freq_cons_dum_5day +
            cond_oc_dum_2inact +
            cond_oc_dum_3unemp +
            susprindum_coc +
            susprindum_pbc +
            susprindum_mar, 
            id=id, data=data_mine_miss_restr_proc2_iiw_after_ph_alt, family= binomial, weights=iiw_after_ph_st, corstr="independence")

Warning in eval(family$initialize): non-integer #successes in a binomial glm!

Code
#In eval(family$initialize) : non-integer #successes in a binomial glm!
#summary(mw)

mwpois_alt <- geeglm( tr_outcome  ~ policonsumo2 +
            edad_al_ing_1 + 
            ano_nac_corr + 
            susinidum_oh +
            susinidum_coc +
            susinidum_pbc +
            susinidum_mar +
            psycom_dum_study +
            psycom_dum_with +
            freq_cons_dum_5day +
            cond_oc_dum_2inact +
            cond_oc_dum_3unemp +
            susprindum_coc +
            susprindum_pbc +
            susprindum_mar, 
            id=id, data=data_mine_miss_restr_proc2_iiw_after_ph_alt, family= poisson(link = "log"), weights=iiw_after_ph_st, corstr="independence")
summary(mwpois)

Call:
geeglm(formula = tr_outcome ~ policonsumo2 + edad_al_ing_1 + 
    ano_nac_corr + susinidum_oh + susinidum_coc + susinidum_pbc + 
    susinidum_mar + psycom_dum_study + psycom_dum_with + freq_cons_dum_5day + 
    cond_oc_dum_2inact + cond_oc_dum_3unemp + susprindum_coc + 
    susprindum_pbc + susprindum_mar, family = poisson(link = "log"), 
    data = data_mine_miss_restr_proc2_iiw_after_ph, weights = iiw_after_ph_st, 
    id = id, corstr = "independence")

 Coefficients:
                   Estimate  Std.err   Wald Pr(>|W|)    
(Intercept)        -6.48845  3.56281   3.32   0.0686 .  
policonsumo2        0.02903  0.01098   6.99   0.0082 ** 
edad_al_ing_1      -0.00520  0.00182   8.13   0.0044 ** 
ano_nac_corr        0.00318  0.00177   3.23   0.0724 .  
susinidum_oh       -0.01208  0.02868   0.18   0.6736    
susinidum_coc       0.00578  0.03446   0.03   0.8668    
susinidum_pbc       0.03984  0.03091   1.66   0.1975    
susinidum_mar       0.01794  0.02878   0.39   0.5331    
psycom_dum_study    0.13012  0.01024 161.40  < 2e-16 ***
psycom_dum_with     0.00609  0.01002   0.37   0.5436    
freq_cons_dum_5day  0.00429  0.00860   0.25   0.6179    
cond_oc_dum_2inact -0.05450  0.01246  19.14  1.2e-05 ***
cond_oc_dum_3unemp -0.03855  0.00943  16.71  4.4e-05 ***
susprindum_coc      0.01730  0.01487   1.35   0.2447    
susprindum_pbc      0.05929  0.01247  22.60  2.0e-06 ***
susprindum_mar      0.01361  0.02142   0.40   0.5252    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Correlation structure = independence 
Estimated Scale Parameters:

            Estimate Std.err
(Intercept)    0.256 0.00339
Number of clusters:   13317  Maximum cluster size: 10 
Code
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

mw_alt_full <- geeglm( tr_outcome  ~ policonsumo2 +
           comp_bpsc_y2_moderate+ 
          comp_bpsc_y3_severe+ 
          edad_al_ing_1+ 
          ano_nac_corr+ 
          esc_dum_rec_3prim+ 
          esc_dum_rec_2high +
          susprindum_oh +
          susprindum_coc +
          susprindum_pbc +
          susprindum_mar+
          freq_cons_dum_5day+
          freq_cons_dum_44to6wk+
          freq_cons_dum_32to3wk+
          freq_cons_dum_21wkmore+
          cond_oc_dum_3unemp+
          cond_oc_dum_2inact+
          viv_dum_illegal+
          viv_dum_own+
          viv_dum_rent+
          viv_dum_temp+
          psycom_dum_with+
          psycom_dum_study+
          susinidum_oh +
          susinidum_coc +
          susinidum_pbc +
          susinidum_mar+
          cohab_dum_alone+
          cohab_dum_fam_or+
          cohab_dum_cpl_child, 
id=id, data=data_mine_miss_restr_proc2_iiw_after_ph_alt, family=binomial, weights=iiw_after_ph_st, corstr="independence") 

Warning in eval(family$initialize): non-integer #successes in a binomial glm!

Code
#In eval(family$initialize) : non-integer #successes in a binomial glm!


mwpois_alt_full <- geeglm( tr_outcome  ~ policonsumo2 +
           comp_bpsc_y2_moderate+ 
          comp_bpsc_y3_severe+ 
          edad_al_ing_1+ 
          ano_nac_corr+ 
          esc_dum_rec_3prim+ 
          esc_dum_rec_2high +
          susprindum_oh +
          susprindum_coc +
          susprindum_pbc +
          susprindum_mar+
          freq_cons_dum_5day+
          freq_cons_dum_44to6wk+
          freq_cons_dum_32to3wk+
          freq_cons_dum_21wkmore+
          cond_oc_dum_3unemp+
          cond_oc_dum_2inact+
          viv_dum_illegal+
          viv_dum_own+
          viv_dum_rent+
          viv_dum_temp+
          psycom_dum_with+
          psycom_dum_study+
          susinidum_oh +
          susinidum_coc +
          susinidum_pbc +
          susinidum_mar+
          cohab_dum_alone+
          cohab_dum_fam_or+
          cohab_dum_cpl_child, 
id=id, data=data_mine_miss_restr_proc2_iiw_after_ph_alt, family= poisson(link = "log"), weights=iiw_after_ph_st, corstr="independence") 

invisible("Model fit")
#broom::glance(mwpois_full)
#QIC(mw)


#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

rbind.data.frame(cbind.data.frame(model="simplest, binomial", broom::tidy(mw_alt, exponentiate = T, conf.int=T)),cbind.data.frame(model="full, binomial", broom::tidy(mw_alt_full, exponentiate = T, conf.int=T)),cbind.data.frame(model="simplest, poisson", broom::tidy(mwpois_alt, exponentiate = T, conf.int=T)),cbind.data.frame(model="full, poisson", broom::tidy(mwpois_alt_full, exponentiate = T, conf.int=T))) %>% dplyr::mutate_at(c("estimate","std.error","conf.low","conf.high"), ~sprintf("%1.2f",.))%>% dplyr::mutate_at(c("p.value"), ~sprintf("%1.4f",.)) %>% dplyr::select(-statistic) %>% 
  { 
    write.table(., file = paste0(getwd(),"/_proposal_grant/2023/geew_alt_240502.csv"), dec=",", sep="\t")
    knitr::kable(dplyr::filter(.,term=="policonsumo2"), size=10, format="markdown", caption="GEE Models, alternative weighting (after correction for PHs)") 
  }
GEE Models, alternative weighting (after correction for PHs)
model term estimate std.error p.value conf.low conf.high
simplest, binomial policonsumo2 1.10 0.05 0.0457 1.00 1.21
full, binomial policonsumo2 1.13 0.05 0.0135 1.02 1.24
simplest, poisson policonsumo2 1.03 0.01 0.0184 1.01 1.06
full, poisson policonsumo2 1.04 0.01 0.0064 1.01 1.07

Stratifying

Code
# Define possible sets of breakpoints
set.seed(2125)
samp1<- sort(c(0,sample.int(max(data_mine_miss_restr_proc2$time)-1, 5, replace = F),max(data_mine_miss_restr_proc2$time)))
set.seed(2121)
samp2<- sort(c(0,sample.int(max(data_mine_miss_restr_proc2$time)-1, 5, replace = F),max(data_mine_miss_restr_proc2$time)))
set.seed(2121e4)
samp3<- sort(c(0,sample.int(max(data_mine_miss_restr_proc2$time)-1, 5, replace = F),max(data_mine_miss_restr_proc2$time)))
set.seed(2121e2)
samp4<- sort(c(0,sample.int(max(data_mine_miss_restr_proc2$time)-1, 5, replace = F),max(data_mine_miss_restr_proc2$time)))
set.seed(2121e3)
samp5<- sort(c(0,sample.int(max(data_mine_miss_restr_proc2$time)-1, 5, replace = F),max(data_mine_miss_restr_proc2$time)))
set.seed(212e3)
samp6<- sort(c(0,sample.int(max(data_mine_miss_restr_proc2$time)-1, 5, replace = F),max(data_mine_miss_restr_proc2$time)))
set.seed(212e4)
samp7<- sort(c(0,sample.int(max(data_mine_miss_restr_proc2$time)-1, 5, replace = F),max(data_mine_miss_restr_proc2$time)))
set.seed(21e4)
samp8<- sort(c(0,sample.int(max(data_mine_miss_restr_proc2$time)-1, 5, replace = F),max(data_mine_miss_restr_proc2$time)))
set.seed(21e6)
samp9<- sort(c(0,sample.int(max(data_mine_miss_restr_proc2$time)-1, 5, replace = F),max(data_mine_miss_restr_proc2$time)))
set.seed(21e8)
samp10<- sort(c(0,sample.int(max(data_mine_miss_restr_proc2$time)-1, 5, replace = F),max(data_mine_miss_restr_proc2$time)))
set.seed(1e2)
samp11<- sort(c(0,sample.int(max(data_mine_miss_restr_proc2$time)-1, 5, replace = F),max(data_mine_miss_restr_proc2$time)))
set.seed(1e3)
samp12<- sort(c(0,sample.int(max(data_mine_miss_restr_proc2$time)-1, 5, replace = F),max(data_mine_miss_restr_proc2$time)))
set.seed(1e4)
samp13<- sort(c(0,sample.int(max(data_mine_miss_restr_proc2$time)-1, 5, replace = F),max(data_mine_miss_restr_proc2$time)))
set.seed(1e7)
samp14<- sort(c(0,sample.int(max(data_mine_miss_restr_proc2$time)-1, 5, replace = F),max(data_mine_miss_restr_proc2$time)))
set.seed(1e5)
samp15<- sort(c(0,sample.int(max(data_mine_miss_restr_proc2$time)-1, 5, replace = F),max(data_mine_miss_restr_proc2$time)))
set.seed(1)
samp16<- sort(c(0,sample.int(max(data_mine_miss_restr_proc2$time)-1, 5, replace = F),max(data_mine_miss_restr_proc2$time)))
set.seed(2)
samp17<- sort(c(0,sample.int(max(data_mine_miss_restr_proc2$time)-1, 5, replace = F),max(data_mine_miss_restr_proc2$time)))
set.seed(3)
samp17<- sort(c(0,sample.int(max(data_mine_miss_restr_proc2$time)-1, 5, replace = F),max(data_mine_miss_restr_proc2$time)))
set.seed(4)
samp18<- sort(c(0,sample.int(max(data_mine_miss_restr_proc2$time)-1, 5, replace = F),max(data_mine_miss_restr_proc2$time)))
set.seed(5)
samp19<- sort(c(0,sample.int(max(data_mine_miss_restr_proc2$time)-1, 5, replace = F),max(data_mine_miss_restr_proc2$time)))
set.seed(6)
samp20<- sort(c(0,sample.int(max(data_mine_miss_restr_proc2$time)-1, 5, replace = F),max(data_mine_miss_restr_proc2$time)))
set.seed(217e2)
samp21<- sort(c(0,sample.int(max(data_mine_miss_restr_proc2$time)-1, 5, replace = F),max(data_mine_miss_restr_proc2$time)))
set.seed(215e2)
samp22<- sort(c(0,sample.int(max(data_mine_miss_restr_proc2$time)-1, 5, replace = F),max(data_mine_miss_restr_proc2$time)))
set.seed(214e2)
samp23<- sort(c(0,sample.int(max(data_mine_miss_restr_proc2$time)-1, 5, replace = F),max(data_mine_miss_restr_proc2$time)))
set.seed(213e2)
samp24<- sort(c(0,sample.int(max(data_mine_miss_restr_proc2$time)-1, 5, replace = F),max(data_mine_miss_restr_proc2$time)))
set.seed(212e2)
samp25<- sort(c(0,sample.int(max(data_mine_miss_restr_proc2$time)-1, 5, replace = F),max(data_mine_miss_restr_proc2$time)))
set.seed(211e2)
samp26<- sort(c(0,sample.int(max(data_mine_miss_restr_proc2$time)-1, 5, replace = F),max(data_mine_miss_restr_proc2$time)))
set.seed(2111e3)
samp27<- sort(c(0,sample.int(max(data_mine_miss_restr_proc2$time)-1, 5, replace = F),max(data_mine_miss_restr_proc2$time)))
set.seed(2112e3)
samp28<- sort(c(0,sample.int(max(data_mine_miss_restr_proc2$time)-1, 5, replace = F),max(data_mine_miss_restr_proc2$time)))
set.seed(2113e3)
samp29<- sort(c(0,sample.int(max(data_mine_miss_restr_proc2$time)-1, 5, replace = F),max(data_mine_miss_restr_proc2$time)))
set.seed(2114e3)
samp30<- sort(c(0,sample.int(max(data_mine_miss_restr_proc2$time)-1, 5, replace = F),max(data_mine_miss_restr_proc2$time)))


breaks_list <- list(
  c(0, 10, 20, 40, 60, 80, max(data_mine_miss_restr_proc2$time)),
  c(0, 15, 30, 45, 60, 75, max(data_mine_miss_restr_proc2$time)),
  c(0, 15, 30, 45, 60, 80, max(data_mine_miss_restr_proc2$time)),
  c(0, 20, 40, 60, 80, 100, max(data_mine_miss_restr_proc2$time)),
    # Unevenly spaced, emphasizing early times
  c(0, 10, 20, 30, 50, 70, max(data_mine_miss_restr_proc2$time)),
  # Long tail distribution
  c(0, 20, 40, 60, 80, 100, 150, max(data_mine_miss_restr_proc2$time)),
  # Quantile-based intervals (ensure you have no NA in `time`)
  quantile(data_mine_miss_restr_proc2$time, probs = seq(0, 1, by = 0.2), na.rm = TRUE),
  samp1, 
  samp2, 
  samp3, 
  samp4, 
  samp5,
  samp6,
  samp7,
  samp8,
  samp9, 
  samp10,
  samp11, 
  samp12, 
  samp13, 
  samp14, 
  samp15,
  samp16,
  samp17,
  samp18,
  samp19, 
  samp20,  
  samp21, 
  samp22, 
  samp23, 
  samp24, 
  samp25,
  samp26,
  samp27,
  samp28,
  samp29, 
  samp30
)

# Prepare to store results
results_strat <- list()

# Loop over the list of breakpoints
for (i in seq_along(breaks_list)) {
  # Create the factor variable with the current set of time intervals
  data_mine_miss_restr_proc2$time_interval2 <- cut(data_mine_miss_restr_proc2$time, breaks_list[[i]], include.lowest = TRUE)
  # Fit the Cox model
  model<-
  cph(as.formula(paste0("Surv(lag_time,time,event)~ 
          cluster(id)+ 
          lag_tr_outcome+
          lag_comp_bpsc_y3_severe+
          lag_less_90d_tr1+
          log_lag_dias_treat_imp_sin_na +
          lag_policonsumo2 + 
          edad_al_ing_1 + 
          ano_nac_corr + 
          susinidum_oh +
          susinidum_coc +
          susinidum_pbc +
          susinidum_mar +
          psycom_dum_study +
          psycom_dum_with +
          freq_cons_dum_5day +
          cond_oc_dum_2inact +
          cond_oc_dum_3unemp +
          susprindum_oh +
          susprindum_coc +
          susprindum_pbc +
          susprindum_mar +
          strat(tipo_de_plan_2_mod)+
          strat(time_interval2)")), 
    data= data_mine_miss_restr_proc2 %>% data.table::as.data.table() %>% data.frame(), x=TRUE, y=TRUE, surv=TRUE, iter.max = 250*4, tol = 1e-6)

  # Perform cox.zph test
  cox_test <- format(cox.zph(model)$table[nrow(cox.zph(model)$table), 1])
  
  # Create a contingency table
  contingency_table <- table(data_mine_miss_restr_proc2$time_interval2, data_mine_miss_restr_proc2$tipo_de_plan_2)
  
  # Store results
  results_strat[[i]] <- list(
    AIC = AIC(model),
    cox_zph = cox_test,
    table = contingency_table,
    fail= model$fail,
    lrtest= paste0(model$stats["Model L.R."],", ",model$stats["d.f."]," p=", model$stats["P"]),
    r2= model$stats["R2"]
  )
}

# You can now inspect 'results' for the output from each set of breakpoints
print(results_strat)
[[1]]
[[1]]$AIC
[1] 201623

[[1]]$cox_zph
[1] "132"

[[1]]$table
          
           basic ambulatory intensive ambulatory residential
  [0,10]               3697                 4941        3018
  (10,20]              1701                 2693        1618
  (20,40]              1766                 3099        1774
  (40,60]               911                 1686         938
  (60,80]               450                  938         517
  (80,135]              270                  642         329

[[1]]$fail
[1] FALSE

[[1]]$lrtest
[1] "244.41760910835, 20 p=0"

[[1]]$r2
    R2 
0.0137 


[[2]]
[[2]]$AIC
[1] 199180

[[2]]$cox_zph
[1] "123"

[[2]]$table
          
           basic ambulatory intensive ambulatory residential
  [0,15]               4692                 6504        3976
  (15,30]              1748                 2892        1728
  (30,45]              1018                 1890         992
  (45,60]               617                 1133         652
  (60,75]               364                  762         411
  (75,135]              356                  818         435

[[2]]$fail
[1] FALSE

[[2]]$lrtest
[1] "236.569110802346, 20 p=0"

[[2]]$r2
    R2 
0.0133 


[[3]]
[[3]]$AIC
[1] 199350

[[3]]$cox_zph
[1] "136"

[[3]]$table
          
           basic ambulatory intensive ambulatory residential
  [0,15]               4692                 6504        3976
  (15,30]              1748                 2892        1728
  (30,45]              1018                 1890         992
  (45,60]               617                 1133         652
  (60,80]               450                  938         517
  (80,135]              270                  642         329

[[3]]$fail
[1] FALSE

[[3]]$lrtest
[1] "234.629002713889, 20 p=0"

[[3]]$r2
    R2 
0.0132 


[[4]]
[[4]]$AIC
[1] 205132

[[4]]$cox_zph
[1] "149"

[[4]]$table
           
            basic ambulatory intensive ambulatory residential
  [0,20]                5398                 7634        4636
  (20,40]               1766                 3099        1774
  (40,60]                911                 1686         938
  (60,80]                450                  938         517
  (80,100]               189                  472         246
  (100,135]               81                  170          83

[[4]]$fail
[1] FALSE

[[4]]$lrtest
[1] "266.667155946809, 20 p=0"

[[4]]$r2
   R2 
0.015 


[[5]]
[[5]]$AIC
[1] 198563

[[5]]$cox_zph
[1] "177"

[[5]]$table
          
           basic ambulatory intensive ambulatory residential
  [0,10]               3697                 4941        3018
  (10,20]              1701                 2693        1618
  (20,30]              1042                 1762        1068
  (30,50]              1246                 2306        1231
  (50,70]               660                 1275         710
  (70,135]              449                 1022         549

[[5]]$fail
[1] FALSE

[[5]]$lrtest
[1] "263.532100785029, 20 p=0"

[[5]]$r2
    R2 
0.0148 


[[6]]
[[6]]$AIC
[1] 205132

[[6]]$cox_zph
[1] "149"

[[6]]$table
           
            basic ambulatory intensive ambulatory residential
  [0,20]                5398                 7634        4636
  (20,40]               1766                 3099        1774
  (40,60]                911                 1686         938
  (60,80]                450                  938         517
  (80,100]               189                  472         246
  (100,135]               81                  170          83
  (135,150]                0                    0           0

[[6]]$fail
[1] FALSE

[[6]]$lrtest
[1] "266.667155946809, 20 p=0"

[[6]]$r2
   R2 
0.015 


[[7]]
[[7]]$AIC
[1] 210729

[[7]]$cox_zph
[1] "646"

[[7]]$table
             
              basic ambulatory intensive ambulatory residential
  [0,4.85]                1887                 2634        1684
  (4.85,11]               2030                 2648        1529
  (11,22.1]               1747                 2777        1658
  (22.1,42.1]             1631                 2899        1668
  (42.1,135]              1500                 3041        1655

[[7]]$fail
[1] FALSE

[[7]]$lrtest
[1] "678.659228396689, 20 p=0"

[[7]]$r2
    R2 
0.0377 


[[8]]
[[8]]$AIC
[1] 236876

[[8]]$cox_zph
[1] "273"

[[8]]$table
           
            basic ambulatory intensive ambulatory residential
  [0,74]                8424                13137        7734
  (74,90]                222                  496         284
  (90,108]               117                  288         130
  (108,117]               26                   63          37
  (117,128]                6                   13           9
  (128,135]                0                    2           0

[[8]]$fail
[1] FALSE

[[8]]$lrtest
[1] "2002.72773700504, 20 p=0"

[[8]]$r2
   R2 
0.107 


[[9]]
[[9]]$AIC
[1] 220361

[[9]]$cox_zph
[1] "289"

[[9]]$table
           
            basic ambulatory intensive ambulatory residential
  [0,18]                5132                 7218        4392
  (18,68]               3174                 5664        3202
  (68,73]                 93                  215         119
  (73,80]                126                  260         152
  (80,119]               266                  632         325
  (119,135]                4                   10           4

[[9]]$fail
[1] FALSE

[[9]]$lrtest
[1] "821.194660197216, 20 p=0"

[[9]]$r2
    R2 
0.0454 


[[10]]
[[10]]$AIC
[1] 202224

[[10]]$cox_zph
[1] "158"

[[10]]$table
          
           basic ambulatory intensive ambulatory residential
  [0,15]               4692                 6504        3976
  (15,25]              1270                 2044        1211
  (25,50]              1724                 3154        1748
  (50,70]               660                 1275         710
  (70,85]               248                  523         304
  (85,135]              201                  499         245

[[10]]$fail
[1] FALSE

[[10]]$lrtest
[1] "242.145208129747, 20 p=0"

[[10]]$r2
    R2 
0.0136 


[[11]]
[[11]]$AIC
[1] 213893

[[11]]$cox_zph
[1] "211"

[[11]]$table
           
            basic ambulatory intensive ambulatory residential
  [0,1]                   88                  177         402
  (1,24]                5779                 8203        4691
  (24,54]               1986                 3617        2021
  (54,70]                493                  980         531
  (70,125]               447                 1019         549
  (125,135]                2                    3           0

[[11]]$fail
[1] FALSE

[[11]]$lrtest
[1] "450.440814013651, 20 p=0"

[[11]]$r2
    R2 
0.0252 


[[12]]
[[12]]$AIC
[1] 220078

[[12]]$cox_zph
[1] "225"

[[12]]$table
           
            basic ambulatory intensive ambulatory residential
  [0,1]                   88                  177         402
  (1,39]                7005                10427        5944
  (39,64]               1100                 2070        1126
  (64,90]                453                  959         546
  (90,122]               147                  360         176
  (122,135]                2                    6           0

[[12]]$fail
[1] FALSE

[[12]]$lrtest
[1] "567.485317411018, 20 p=0"

[[12]]$r2
    R2 
0.0316 


[[13]]
[[13]]$AIC
[1] 224070

[[13]]$cox_zph
[1] "223"

[[13]]$table
           
            basic ambulatory intensive ambulatory residential
  [0,42]                7292                10953        6531
  (42,78]               1198                 2340        1295
  (78,84]                 90                  182         104
  (84,126]               215                  521         264
  (126,128]                0                    1           0
  (128,135]                0                    2           0

[[13]]$fail
[1] FALSE

[[13]]$lrtest
[1] "892.089426955208, 20 p=0"

[[13]]$r2
    R2 
0.0492 


[[14]]
[[14]]$AIC
[1] 231083

[[14]]$cox_zph
[1] "244"

[[14]]$table
           
            basic ambulatory intensive ambulatory residential
  [0,11]                3917                 5282        3213
  (11,78]               4573                 8011        4613
  (78,86]                115                  230         143
  (86,119]               186                  466         221
  (119,129]                4                    9           4
  (129,135]                0                    1           0

[[14]]$fail
[1] FALSE

[[14]]$lrtest
[1] "1732.33594561392, 20 p=0"

[[14]]$r2
    R2 
0.0934 


[[15]]
[[15]]$AIC
[1] 222381

[[15]]$cox_zph
[1] "241"

[[15]]$table
           
            basic ambulatory intensive ambulatory residential
  [0,15]                4692                 6504        3976
  (15,65]               3536                 6218        3521
  (65,90]                418                  911         521
  (90,107]               112                  275         126
  (107,120]               34                   81          50
  (120,135]                3                   10           0

[[15]]$fail
[1] FALSE

[[15]]$lrtest
[1] "957.269989653141, 20 p=0"

[[15]]$r2
    R2 
0.0527 


[[16]]
[[16]]$AIC
[1] 216166

[[16]]$cox_zph
[1] "317"

[[16]]$table
           
            basic ambulatory intensive ambulatory residential
  [0,29]                6350                 9232        5603
  (29,51]               1385                 2542        1372
  (51,99]                974                 2042        1129
  (99,110]                61                  125          56
  (110,131]               25                   57          34
  (131,135]                0                    1           0

[[16]]$fail
[1] FALSE

[[16]]$lrtest
[1] "526.069504202693, 20 p=0"

[[16]]$r2
    R2 
0.0293 


[[17]]
[[17]]$AIC
[1] 241240

[[17]]$cox_zph
[1] "336"

[[17]]$table
           
            basic ambulatory intensive ambulatory residential
  [0,88]                8623                13584        7988
  (88,93]                 47                  113          58
  (93,110]               100                  244         114
  (110,129]               25                   57          34
  (129,133]                0                    0           0
  (133,135]                0                    1           0

[[17]]$fail
[1] FALSE

[[17]]$lrtest
[1] "2685.26883503475, 20 p=0"

[[17]]$r2
   R2 
0.141 


[[18]]
[[18]]$AIC
[1] 227090

[[18]]$cox_zph
[1] "240"

[[18]]$table
           
            basic ambulatory intensive ambulatory residential
  [0,4]                 1474                 2139        1422
  (4,55]                6419                 9938        5742
  (55,70]                453                  900         481
  (70,102]               387                  869         477
  (102,112]               47                  111          43
  (112,135]               15                   42          29

[[18]]$fail
[1] FALSE

[[18]]$lrtest
[1] "1063.35690842738, 20 p=0"

[[18]]$r2
    R2 
0.0584 


[[19]]
[[19]]$AIC
[1] 204946

[[19]]$cox_zph
[1] "222"

[[19]]$table
          
           basic ambulatory intensive ambulatory residential
  [0,29]               6350                 9232        5603
  (29,43]               998                 1837         989
  (43,51]               387                  705         383
  (51,68]               571                 1108         619
  (68,88]               317                  702         394
  (88,135]              172                  415         206

[[19]]$fail
[1] FALSE

[[19]]$lrtest
[1] "309.440397133556, 20 p=0"

[[19]]$r2
    R2 
0.0174 


[[20]]
[[20]]$AIC
[1] 207600

[[20]]$cox_zph
[1] "163"

[[20]]$table
          
           basic ambulatory intensive ambulatory residential
  [0,9]                3455                 4593        2807
  (9,30]               2985                 4803        2897
  (30,45]              1018                 1890         992
  (45,79]              1052                 2043        1148
  (79,84]                70                  146          86
  (84,135]              215                  524         264

[[20]]$fail
[1] FALSE

[[20]]$lrtest
[1] "418.016287462291, 20 p=0"

[[20]]$r2
    R2 
0.0234 


[[21]]
[[21]]$AIC
[1] 227986

[[21]]$cox_zph
[1] "172"

[[21]]$table
          
           basic ambulatory intensive ambulatory residential
  [0,6]                2395                 3270        2033
  (6,9]                1060                 1323         774
  (9,70]               4891                 8384        4838
  (70,80]               179                  380         220
  (80,92]               134                  317         175
  (92,135]              136                  325         154

[[21]]$fail
[1] FALSE

[[21]]$lrtest
[1] "1537.47392012289, 20 p=0"

[[21]]$r2
    R2 
0.0833 


[[22]]
[[22]]$AIC
[1] 228537

[[22]]$cox_zph
[1] "232"

[[22]]$table
           
            basic ambulatory intensive ambulatory residential
  [0,54]                7853                11997        7114
  (54,76]                609                 1226         666
  (76,106]               291                  678         360
  (106,107]                5                    7           4
  (107,131]               37                   90          50
  (131,135]                0                    1           0

[[22]]$fail
[1] FALSE

[[22]]$lrtest
[1] "1075.1562108639, 20 p=0"

[[22]]$r2
   R2 
0.059 


[[23]]
[[23]]$AIC
[1] 209837

[[23]]$cox_zph
[1] "168"

[[23]]$table
           
            basic ambulatory intensive ambulatory residential
  [0,14]                4509                 6237        3819
  (14,43]               2839                 4832        2773
  (43,51]                387                  705         383
  (51,68]                571                 1108         619
  (68,129]               489                 1116         600
  (129,135]                0                    1           0

[[23]]$fail
[1] FALSE

[[23]]$lrtest
[1] "453.468024794711, 20 p=0"

[[23]]$r2
    R2 
0.0253 


[[24]]
[[24]]$AIC
[1] 209169

[[24]]$cox_zph
[1] "224"

[[24]]$table
           
            basic ambulatory intensive ambulatory residential
  [0,5]                 1947                 2708        1720
  (5,20]                3451                 4926        2916
  (20,36]               1499                 2599        1538
  (36,74]               1527                 2904        1560
  (74,107]               334                  771         410
  (107,135]               37                   91          50

[[24]]$fail
[1] FALSE

[[24]]$lrtest
[1] "440.86968850813, 20 p=0"

[[24]]$r2
    R2 
0.0246 


[[25]]
[[25]]$AIC
[1] 225610

[[25]]$cox_zph
[1] "260"

[[25]]$table
           
            basic ambulatory intensive ambulatory residential
  [0,3]                  915                 1436        1091
  (3,51]                6820                10338        5884
  (51,71]                629                 1242         700
  (71,75]                 75                  165          84
  (75,115]               347                  789         421
  (115,135]                9                   29          14

[[25]]$fail
[1] FALSE

[[25]]$lrtest
[1] "908.308044379839, 20 p=0"

[[25]]$r2
    R2 
0.0501 


[[26]]
[[26]]$AIC
[1] 220425

[[26]]$cox_zph
[1] "244"

[[26]]$table
           
            basic ambulatory intensive ambulatory residential
  [0,41]                7224                10837        6468
  (41,66]               1024                 1948        1067
  (66,71]                116                  231         140
  (71,107]               394                  892         469
  (107,121]               35                   82          50
  (121,135]                2                    9           0

[[26]]$fail
[1] FALSE

[[26]]$lrtest
[1] "695.022012121801, 20 p=0"

[[26]]$r2
    R2 
0.0386 


[[27]]
[[27]]$AIC
[1] 218142

[[27]]$cox_zph
[1] "152"

[[27]]$table
           
            basic ambulatory intensive ambulatory residential
  [0,10]                3697                 4941        3018
  (10,53]               4118                 6978        4055
  (53,60]                260                  500         275
  (60,78]                415                  874         478
  (78,100]               224                  536         285
  (100,135]               81                  170          83

[[27]]$fail
[1] FALSE

[[27]]$lrtest
[1] "743.24174347092, 20 p=0"

[[27]]$r2
    R2 
0.0412 


[[28]]
[[28]]$AIC
[1] 218161

[[28]]$cox_zph
[1] "252"

[[28]]$table
           
            basic ambulatory intensive ambulatory residential
  [0,33]                6697                 9826        5947
  (33,36]                200                  407         227
  (36,76]               1565                 2990        1606
  (76,77]                 15                   23          20
  (77,122]               316                  747         394
  (122,135]                2                    6           0

[[28]]$fail
[1] FALSE

[[28]]$lrtest
[1] "658.233573928737, 20 p=0"

[[28]]$r2
    R2 
0.0366 


[[29]]
[[29]]$AIC
[1] 216388

[[29]]$cox_zph
[1] "236"

[[29]]$table
           
            basic ambulatory intensive ambulatory residential
  [0,36]                6897                10233        6174
  (36,52]                873                 1621         853
  (52,86]                835                 1669         942
  (86,102]               128                  323         153
  (102,132]               62                  152          72
  (132,135]                0                    1           0

[[29]]$fail
[1] FALSE

[[29]]$lrtest
[1] "513.853596449771, 20 p=0"

[[29]]$r2
    R2 
0.0287 


[[30]]
[[30]]$AIC
[1] 197479

[[30]]$cox_zph
[1] "396"

[[30]]$table
          
           basic ambulatory intensive ambulatory residential
  [0,13]               4335                 5941        3619
  (13,17]               657                 1039         637
  (17,28]              1271                 2094        1259
  (28,36]               634                 1159         659
  (36,52]               873                 1621         853
  (52,135]             1025                 2145        1167

[[30]]$fail
[1] FALSE

[[30]]$lrtest
[1] "316.936203734804, 20 p=0"

[[30]]$r2
    R2 
0.0178 


[[31]]
[[31]]$AIC
[1] 224694

[[31]]$cox_zph
[1] "253"

[[31]]$table
           
            basic ambulatory intensive ambulatory residential
  [0,50]                7686                11702        6935
  (50,62]                449                  838         469
  (62,96]                555                 1202         672
  (96,106]                63                  159          64
  (106,116]               34                   77          44
  (116,135]                8                   21          10

[[31]]$fail
[1] FALSE

[[31]]$lrtest
[1] "908.547777739586, 20 p=0"

[[31]]$r2
    R2 
0.0501 


[[32]]
[[32]]$AIC
[1] 232440

[[32]]$cox_zph
[1] "249"

[[32]]$table
           
            basic ambulatory intensive ambulatory residential
  [0,62]                8135                12540        7404
  (62,73]                264                  557         309
  (73,113]               382                  861         455
  (113,120]               11                   31          26
  (120,129]                3                    9           0
  (129,135]                0                    1           0

[[32]]$fail
[1] FALSE

[[32]]$lrtest
[1] "1438.46574364958, 20 p=0"

[[32]]$r2
    R2 
0.0782 


[[33]]
[[33]]$AIC
[1] 229114

[[33]]$cox_zph
[1] "262"

[[33]]$table
           
            basic ambulatory intensive ambulatory residential
  [0,55]                7893                12077        7164
  (55,81]                645                 1318         716
  (81,82]                 13                   35          19
  (82,107]               207                  478         245
  (107,124]               35                   88          50
  (124,135]                2                    3           0

[[33]]$fail
[1] FALSE

[[33]]$lrtest
[1] "1131.15766450859, 20 p=0"

[[33]]$r2
   R2 
0.062 


[[34]]
[[34]]$AIC
[1] 207443

[[34]]$cox_zph
[1] "324"

[[34]]$table
           
            basic ambulatory intensive ambulatory residential
  [0,16]                4837                 6759        4118
  (16,38]               2194                 3717        2174
  (38,43]                317                  593         300
  (43,51]                387                  705         383
  (51,132]              1060                 2224        1219
  (132,135]                0                    1           0

[[34]]$fail
[1] FALSE

[[34]]$lrtest
[1] "488.713657127577, 20 p=0"

[[34]]$r2
    R2 
0.0273 


[[35]]
[[35]]$AIC
[1] 215882

[[35]]$cox_zph
[1] "685"

[[35]]$table
           
            basic ambulatory intensive ambulatory residential
  [0,8]                 3151                 4168        2554
  (8,28]                3112                 4906        2961
  (28,35]                568                 1010         582
  (35,112]              1949                 3873        2068
  (112,114]                5                    9          10
  (114,135]               10                   33          19

[[35]]$fail
[1] FALSE

[[35]]$lrtest
[1] "872.879308136587, 20 p=0"

[[35]]$r2
    R2 
0.0482 


[[36]]
[[36]]$AIC
[1] 226868

[[36]]$cox_zph
[1] "466"

[[36]]$table
           
            basic ambulatory intensive ambulatory residential
  [0,1]                   88                  177         402
  (1,34]                6681                 9783        5633
  (34,98]               1932                 3827        2062
  (98,128]                94                  210          97
  (128,132]                0                    1           0
  (132,135]                0                    1           0

[[36]]$fail
[1] FALSE

[[36]]$lrtest
[1] "1064.25334982859, 20 p=0"

[[36]]$r2
    R2 
0.0584 


[[37]]
[[37]]$AIC
[1] 219085

[[37]]$cox_zph
[1] "185"

[[37]]$table
           
            basic ambulatory intensive ambulatory residential
  [0,4]                 1474                 2139        1422
  (4,10]                2223                 2802        1596
  (10,45]               3761                 6345        3678
  (45,75]                981                 1895        1063
  (75,124]               354                  815         435
  (124,135]                2                    3           0

[[37]]$fail
[1] FALSE

[[37]]$lrtest
[1] "697.807880941953, 20 p=0"

[[37]]$r2
    R2 
0.0387 
Code
print(results_strat[[2]])
$AIC
[1] 199180

$cox_zph
[1] "123"

$table
          
           basic ambulatory intensive ambulatory residential
  [0,15]               4692                 6504        3976
  (15,30]              1748                 2892        1728
  (30,45]              1018                 1890         992
  (45,60]               617                 1133         652
  (60,75]               364                  762         411
  (75,135]              356                  818         435

$fail
[1] FALSE

$lrtest
[1] "236.569110802346, 20 p=0"

$r2
    R2 
0.0133 
Code
#117
invisible("El original X2= 430.6824, df=18, p< 2e-16; ")

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
invisible("SELECTY INTERVAL TIME TO STRATIFY")
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
invisible("E:/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/selected_models_strat.R")
# Create a factor variable with time intervals
data_mine_miss_restr_proc2$time_interval3 <- cut(data_mine_miss_restr_proc2$time, 
                                                 breaks_list[[17]][c(1:3,7)], 
                                                 include.lowest = TRUE)

#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:
model_after_time_strat<-
cph(Surv(lag_time,time,event)~
          cluster(id)+ 
          lag_tr_outcome+
          lag_comp_bpsc_y3_severe+
          lag_less_90d_tr1+
          log_lag_dias_treat_imp_sin_na +
          lag_policonsumo2 + 
          edad_al_ing_1 + 
          ano_nac_corr + 
          susinidum_oh +
          susinidum_coc +
          susinidum_pbc +
          susinidum_mar +
          psycom_dum_study +
          psycom_dum_with +
          freq_cons_dum_5day +
          cond_oc_dum_2inact +
          cond_oc_dum_3unemp +
          susprindum_oh +
          susprindum_coc +
          susprindum_pbc +
          susprindum_mar +
          strat(tipo_de_plan_2_mod)+
          strat(time_interval3),
    data= data_mine_miss_restr_proc2 %>% data.table::as.data.table() %>% data.frame(), 
    x=TRUE, y=TRUE, surv=TRUE, iter.max = 250*4, tol = 1e-6)
# 
model_after_time_strat
Frequencies of Missing Values Due to Each Variable
  Surv(lag_time, time, event)                   cluster(id) 
                        13317                             0 
               lag_tr_outcome       lag_comp_bpsc_y3_severe 
                            0                             0 
             lag_less_90d_tr1 log_lag_dias_treat_imp_sin_na 
                            0                         13317 
             lag_policonsumo2                 edad_al_ing_1 
                            0                             0 
                 ano_nac_corr                  susinidum_oh 
                            0                             0 
                susinidum_coc                 susinidum_pbc 
                            0                             0 
                susinidum_mar              psycom_dum_study 
                            0                             0 
              psycom_dum_with            freq_cons_dum_5day 
                            0                             0 
           cond_oc_dum_2inact            cond_oc_dum_3unemp 
                            0                             0 
                susprindum_oh                susprindum_coc 
                            0                             0 
               susprindum_pbc                susprindum_mar 
                            0                             0 
           tipo_de_plan_2_mod                time_interval3 
                            0                             0 

Cox Proportional Hazards Model
 
 cph(formula = Surv(lag_time, time, event) ~ cluster(id) + lag_tr_outcome + 
     lag_comp_bpsc_y3_severe + lag_less_90d_tr1 + log_lag_dias_treat_imp_sin_na + 
     lag_policonsumo2 + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
     susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_study + 
     psycom_dum_with + freq_cons_dum_5day + cond_oc_dum_2inact + 
     cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
     susprindum_mar + strat(tipo_de_plan_2_mod) + strat(time_interval3), 
     data = data_mine_miss_restr_proc2 %>% data.table::as.data.table() %>% 
         data.frame(), x = TRUE, y = TRUE, iter.max = 250 * 4, 
     tol = 1e-06, surv = TRUE)
 
 
                                                                     Status
 Stratum                                                              No Event
   tipo_de_plan_2_mod=basic ambulatory.time_interval3=[0,88]                 0
   tipo_de_plan_2_mod=GP intensive ambulatory.time_interval3=[0,88]          0
   tipo_de_plan_2_mod=GP residential.time_interval3=[0,88]                   0
   tipo_de_plan_2_mod=WO intensive ambulatory.time_interval3=[0,88]          0
   tipo_de_plan_2_mod=WO residential.time_interval3=[0,88]                   0
   tipo_de_plan_2_mod=basic ambulatory.time_interval3=(88,93]                0
   tipo_de_plan_2_mod=GP intensive ambulatory.time_interval3=(88,93]         0
   tipo_de_plan_2_mod=GP residential.time_interval3=(88,93]                  0
   tipo_de_plan_2_mod=WO intensive ambulatory.time_interval3=(88,93]         0
   tipo_de_plan_2_mod=WO residential.time_interval3=(88,93]                  0
   tipo_de_plan_2_mod=basic ambulatory.time_interval3=(93,135]               0
   tipo_de_plan_2_mod=GP intensive ambulatory.time_interval3=(93,135]        0
   tipo_de_plan_2_mod=GP residential.time_interval3=(93,135]                 0
   tipo_de_plan_2_mod=WO intensive ambulatory.time_interval3=(93,135]        0
   tipo_de_plan_2_mod=WO residential.time_interval3=(93,135]                 0
                                                                     Status
 Stratum                                                              Event
   tipo_de_plan_2_mod=basic ambulatory.time_interval3=[0,88]           5356
   tipo_de_plan_2_mod=GP intensive ambulatory.time_interval3=[0,88]    6212
   tipo_de_plan_2_mod=GP residential.time_interval3=[0,88]             2895
   tipo_de_plan_2_mod=WO intensive ambulatory.time_interval3=[0,88]     964
   tipo_de_plan_2_mod=WO residential.time_interval3=[0,88]             1451
   tipo_de_plan_2_mod=basic ambulatory.time_interval3=(88,93]            77
   tipo_de_plan_2_mod=GP intensive ambulatory.time_interval3=(88,93]     74
   tipo_de_plan_2_mod=GP residential.time_interval3=(88,93]              44
   tipo_de_plan_2_mod=WO intensive ambulatory.time_interval3=(88,93]     10
   tipo_de_plan_2_mod=WO residential.time_interval3=(88,93]              13
   tipo_de_plan_2_mod=basic ambulatory.time_interval3=(93,135]          200
   tipo_de_plan_2_mod=GP intensive ambulatory.time_interval3=(93,135]   213
   tipo_de_plan_2_mod=GP residential.time_interval3=(93,135]             87
   tipo_de_plan_2_mod=WO intensive ambulatory.time_interval3=(93,135]    41
   tipo_de_plan_2_mod=WO residential.time_interval3=(93,135]             34
 
                        Model Tests       Discrimination    
                                                 Indexes    
 Obs    17671    LR chi2    2722.85       R2       0.143    
 Events 17671    d.f.            20    R2(20,17671)0.142    
 Center   402    Pr(> chi2)  0.0000       Dxy      0.256    
                 Score chi2 2731.11                         
                 Pr(> chi2)  0.0000                         
 
                               Coef    S.E.   Wald Z Pr(>|Z|)
 lag_tr_outcome                 0.1374 0.0182  7.55  <0.0001 
 lag_comp_bpsc_y3_severe        0.0508 0.0169  3.00  0.0027  
 lag_less_90d_tr1               0.1048 0.0252  4.16  <0.0001 
 log_lag_dias_treat_imp_sin_na -0.0197 0.0110 -1.79  0.0733  
 lag_policonsumo2              -0.0206 0.0191 -1.07  0.2828  
 edad_al_ing_1                  0.1987 0.0038 52.28  <0.0001 
 ano_nac_corr                   0.1997 0.0037 53.44  <0.0001 
 susinidum_oh                   0.0440 0.0483  0.91  0.3623  
 susinidum_coc                  0.1127 0.0595  1.89  0.0582  
 susinidum_pbc                  0.1409 0.0545  2.58  0.0098  
 susinidum_mar                  0.1610 0.0492  3.27  0.0011  
 psycom_dum_study               0.0132 0.0231  0.57  0.5691  
 psycom_dum_with                0.0145 0.0164  0.89  0.3759  
 freq_cons_dum_5day             0.0094 0.0160  0.59  0.5556  
 cond_oc_dum_2inact             0.0757 0.0218  3.47  0.0005  
 cond_oc_dum_3unemp             0.0841 0.0176  4.79  <0.0001 
 susprindum_oh                 -0.0798 0.0679 -1.18  0.2398  
 susprindum_coc                -0.0757 0.0687 -1.10  0.2708  
 susprindum_pbc                -0.0578 0.0675 -0.86  0.3917  
 susprindum_mar                -0.0801 0.0744 -1.08  0.2814  
 
Code
# Cox Proportional Hazards Model
# 
# cph(formula = Surv(lag_time, time, event) ~ cluster(id) + lag_tr_outcome + 
#     lag_comp_bpsc_y3_severe + lag_less_90d_tr1 + log_lag_dias_treat_imp_sin_na + 
#     lag_policonsumo2 + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
#     susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_study + 
#     psycom_dum_with + freq_cons_dum_5day + cond_oc_dum_2inact + 
#     cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
#     susprindum_mar + strat(tipo_de_plan_2_mod) + strat(time_interval3), 
#     data = data_mine_miss_restr_proc2 %>% data.table::as.data.table() %>% 
#         data.frame(), x = TRUE, y = TRUE, iter.max = 250 * 4, 
#     tol = 1e-06, surv = TRUE)
# 
# 
#                                                                     Status
# Stratum                                                              No Event Event
#   tipo_de_plan_2_mod=basic ambulatory.time_interval3=[0,88]                 0  5356
#   tipo_de_plan_2_mod=GP intensive ambulatory.time_interval3=[0,88]          0  6212
#   tipo_de_plan_2_mod=GP residential.time_interval3=[0,88]                   0  2895
#   tipo_de_plan_2_mod=WO intensive ambulatory.time_interval3=[0,88]          0   964
#   tipo_de_plan_2_mod=WO residential.time_interval3=[0,88]                   0  1451
#   tipo_de_plan_2_mod=basic ambulatory.time_interval3=(88,93]                0    77
#   tipo_de_plan_2_mod=GP intensive ambulatory.time_interval3=(88,93]         0    74
#   tipo_de_plan_2_mod=GP residential.time_interval3=(88,93]                  0    44
#   tipo_de_plan_2_mod=WO intensive ambulatory.time_interval3=(88,93]         0    10
#   tipo_de_plan_2_mod=WO residential.time_interval3=(88,93]                  0    13
#   tipo_de_plan_2_mod=basic ambulatory.time_interval3=(93,135]               0   200
#   tipo_de_plan_2_mod=GP intensive ambulatory.time_interval3=(93,135]        0   213
#   tipo_de_plan_2_mod=GP residential.time_interval3=(93,135]                 0    87
#   tipo_de_plan_2_mod=WO intensive ambulatory.time_interval3=(93,135]        0    41
#   tipo_de_plan_2_mod=WO residential.time_interval3=(93,135]                 0    34
# 
#                        Model Tests       Discrimination    
#                                                 Indexes    
# Obs    17671    LR chi2    2722.85       R2       0.143    
# Events 17671    d.f.            20    R2(20,17671)0.142    
# Center   402    Pr(> chi2)  0.0000       Dxy      0.256    
#                 Score chi2 2731.11                         
#                 Pr(> chi2)  0.0000                         
# 
#                               Coef    S.E.   Wald Z Pr(>|Z|)
# lag_tr_outcome                 0.1374 0.0182  7.55  <0.0001 
# lag_comp_bpsc_y3_severe        0.0508 0.0169  3.00  0.0027  
# lag_less_90d_tr1               0.1048 0.0252  4.16  <0.0001 
# log_lag_dias_treat_imp_sin_na -0.0197 0.0110 -1.79  0.0733  
# lag_policonsumo2              -0.0206 0.0191 -1.07  0.2828  
# edad_al_ing_1                  0.1987 0.0038 52.28  <0.0001 
# ano_nac_corr                   0.1997 0.0037 53.44  <0.0001 
# susinidum_oh                   0.0440 0.0483  0.91  0.3623  
# susinidum_coc                  0.1127 0.0595  1.89  0.0582  
# susinidum_pbc                  0.1409 0.0545  2.58  0.0098  
# susinidum_mar                  0.1610 0.0492  3.27  0.0011  
# psycom_dum_study               0.0132 0.0231  0.57  0.5691  
# psycom_dum_with                0.0145 0.0164  0.89  0.3759  
# freq_cons_dum_5day             0.0094 0.0160  0.59  0.5556  
# cond_oc_dum_2inact             0.0757 0.0218  3.47  0.0005  
# cond_oc_dum_3unemp             0.0841 0.0176  4.79  <0.0001 
# susprindum_oh                 -0.0798 0.0679 -1.18  0.2398  
# susprindum_coc                -0.0757 0.0687 -1.10  0.2708  
# susprindum_pbc                -0.0578 0.0675 -0.86  0.3917  
# susprindum_mar                -0.0801 0.0744 -1.08  0.2814  

cox.zph(model_after_time_strat)
                                chisq df       p
lag_tr_outcome                 60.354  1 7.9e-15
lag_comp_bpsc_y3_severe        20.528  1 5.9e-06
lag_less_90d_tr1              151.524  1 < 2e-16
log_lag_dias_treat_imp_sin_na 160.971  1 < 2e-16
lag_policonsumo2                5.712  1 0.01685
edad_al_ing_1                  31.506  1 2.0e-08
ano_nac_corr                   19.244  1 1.2e-05
susinidum_oh                   35.309  1 2.8e-09
susinidum_coc                   0.931  1 0.33456
susinidum_pbc                  10.487  1 0.00120
susinidum_mar                  17.059  1 3.6e-05
psycom_dum_study               51.800  1 6.1e-13
psycom_dum_with                18.230  1 2.0e-05
freq_cons_dum_5day              3.577  1 0.05860
cond_oc_dum_2inact              3.270  1 0.07055
cond_oc_dum_3unemp              7.459  1 0.00631
susprindum_oh                  16.966  1 3.8e-05
susprindum_coc                  0.186  1 0.66631
susprindum_pbc                 12.610  1 0.00038
susprindum_mar                  1.337  1 0.24758
GLOBAL                        323.981 20 < 2e-16
Code
#                                 chisq df       p
# lag_tr_outcome                 60.354  1 7.9e-15
# lag_comp_bpsc_y3_severe        20.528  1 5.9e-06
# lag_less_90d_tr1              151.524  1 < 2e-16
# log_lag_dias_treat_imp_sin_na 160.971  1 < 2e-16
# lag_policonsumo2                5.712  1 0.01685
# edad_al_ing_1                  31.506  1 2.0e-08
# ano_nac_corr                   19.244  1 1.2e-05
# susinidum_oh                   35.309  1 2.8e-09
# susinidum_coc                   0.931  1 0.33456
# susinidum_pbc                  10.487  1 0.00120
# susinidum_mar                  17.059  1 3.6e-05
# psycom_dum_study               51.800  1 6.1e-13
# psycom_dum_with                18.230  1 2.0e-05
# freq_cons_dum_5day              3.577  1 0.05860
# cond_oc_dum_2inact              3.270  1 0.07055
# cond_oc_dum_3unemp              7.459  1 0.00631
# susprindum_oh                  16.966  1 3.8e-05
# susprindum_coc                  0.186  1 0.66631
# susprindum_pbc                 12.610  1 0.00038
# susprindum_mar                  1.337  1 0.24758
# GLOBAL                        323.981 20 < 2e-16

table(data_mine_miss_restr_proc2$time_interval3, data_mine_miss_restr_proc2$tipo_de_plan_2_mod)
          
           basic ambulatory GP intensive ambulatory GP residential
  [0,88]               9716                   11210           5073
  (88,93]                77                      74             44
  (93,135]              200                     213             87
          
           WO intensive ambulatory WO residential
  [0,88]                      1709           2487
  (88,93]                       10             13
  (93,135]                      41             34
Code
  #          basic ambulatory GP intensive ambulatory GP residential WO intensive ambulatory WO residential
  # [0,88]               9716                   11210           5073                    1709           2487
  # (88,93]                77                      74             44                      10             13
  # (93,135]              200                     213             87                      41             34

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
data_mine_miss_restr_proc2$time_interval3_alt <- cut(data_mine_miss_restr_proc2$time, 
                                                 breaks_list[[5]], 
                                                 include.lowest = TRUE)

model_after_time_strat_alt<-
cph(Surv(lag_time,time,event)~
          cluster(id)+ 
          lag_tr_outcome+
          lag_comp_bpsc_y3_severe+
          lag_less_90d_tr1+
          log_lag_dias_treat_imp_sin_na +
          lag_policonsumo2 + 
          edad_al_ing_1 + 
          ano_nac_corr + 
          susinidum_oh +
          susinidum_coc +
          susinidum_pbc +
          susinidum_mar +
          psycom_dum_study +
          psycom_dum_with +
          freq_cons_dum_5day +
          cond_oc_dum_2inact +
          cond_oc_dum_3unemp +
          susprindum_oh +
          susprindum_coc +
          susprindum_pbc +
          susprindum_mar +
          strat(tipo_de_plan_2_mod)+
          strat(time_interval3_alt),
    data= data_mine_miss_restr_proc2 %>% data.table::as.data.table() %>% data.frame(), 
    x=TRUE, y=TRUE, surv=TRUE, iter.max = 250*4, tol = 1e-6)
# 
model_after_time_strat_alt
Frequencies of Missing Values Due to Each Variable
  Surv(lag_time, time, event)                   cluster(id) 
                        13317                             0 
               lag_tr_outcome       lag_comp_bpsc_y3_severe 
                            0                             0 
             lag_less_90d_tr1 log_lag_dias_treat_imp_sin_na 
                            0                         13317 
             lag_policonsumo2                 edad_al_ing_1 
                            0                             0 
                 ano_nac_corr                  susinidum_oh 
                            0                             0 
                susinidum_coc                 susinidum_pbc 
                            0                             0 
                susinidum_mar              psycom_dum_study 
                            0                             0 
              psycom_dum_with            freq_cons_dum_5day 
                            0                             0 
           cond_oc_dum_2inact            cond_oc_dum_3unemp 
                            0                             0 
                susprindum_oh                susprindum_coc 
                            0                             0 
               susprindum_pbc                susprindum_mar 
                            0                             0 
           tipo_de_plan_2_mod            time_interval3_alt 
                            0                             0 

Cox Proportional Hazards Model
 
 cph(formula = Surv(lag_time, time, event) ~ cluster(id) + lag_tr_outcome + 
     lag_comp_bpsc_y3_severe + lag_less_90d_tr1 + log_lag_dias_treat_imp_sin_na + 
     lag_policonsumo2 + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
     susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_study + 
     psycom_dum_with + freq_cons_dum_5day + cond_oc_dum_2inact + 
     cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
     susprindum_mar + strat(tipo_de_plan_2_mod) + strat(time_interval3_alt), 
     data = data_mine_miss_restr_proc2 %>% data.table::as.data.table() %>% 
         data.frame(), x = TRUE, y = TRUE, iter.max = 250 * 4, 
     tol = 1e-06, surv = TRUE)
 
 
                                                                         Status
 Stratum                                                                  No Event
   tipo_de_plan_2_mod=basic ambulatory.time_interval3_alt=[0,10]                 0
   tipo_de_plan_2_mod=GP intensive ambulatory.time_interval3_alt=[0,10]          0
   tipo_de_plan_2_mod=GP residential.time_interval3_alt=[0,10]                   0
   tipo_de_plan_2_mod=WO intensive ambulatory.time_interval3_alt=[0,10]          0
   tipo_de_plan_2_mod=WO residential.time_interval3_alt=[0,10]                   0
   tipo_de_plan_2_mod=basic ambulatory.time_interval3_alt=(10,20]                0
   tipo_de_plan_2_mod=GP intensive ambulatory.time_interval3_alt=(10,20]         0
   tipo_de_plan_2_mod=GP residential.time_interval3_alt=(10,20]                  0
   tipo_de_plan_2_mod=WO intensive ambulatory.time_interval3_alt=(10,20]         0
   tipo_de_plan_2_mod=WO residential.time_interval3_alt=(10,20]                  0
   tipo_de_plan_2_mod=basic ambulatory.time_interval3_alt=(20,30]                0
   tipo_de_plan_2_mod=GP intensive ambulatory.time_interval3_alt=(20,30]         0
   tipo_de_plan_2_mod=GP residential.time_interval3_alt=(20,30]                  0
   tipo_de_plan_2_mod=WO intensive ambulatory.time_interval3_alt=(20,30]         0
   tipo_de_plan_2_mod=WO residential.time_interval3_alt=(20,30]                  0
   tipo_de_plan_2_mod=basic ambulatory.time_interval3_alt=(30,50]                0
   tipo_de_plan_2_mod=GP intensive ambulatory.time_interval3_alt=(30,50]         0
   tipo_de_plan_2_mod=GP residential.time_interval3_alt=(30,50]                  0
   tipo_de_plan_2_mod=WO intensive ambulatory.time_interval3_alt=(30,50]         0
   tipo_de_plan_2_mod=WO residential.time_interval3_alt=(30,50]                  0
   tipo_de_plan_2_mod=basic ambulatory.time_interval3_alt=(50,70]                0
   tipo_de_plan_2_mod=GP intensive ambulatory.time_interval3_alt=(50,70]         0
   tipo_de_plan_2_mod=GP residential.time_interval3_alt=(50,70]                  0
   tipo_de_plan_2_mod=WO intensive ambulatory.time_interval3_alt=(50,70]         0
   tipo_de_plan_2_mod=WO residential.time_interval3_alt=(50,70]                  0
   tipo_de_plan_2_mod=basic ambulatory.time_interval3_alt=(70,135]               0
   tipo_de_plan_2_mod=GP intensive ambulatory.time_interval3_alt=(70,135]        0
   tipo_de_plan_2_mod=GP residential.time_interval3_alt=(70,135]                 0
   tipo_de_plan_2_mod=WO intensive ambulatory.time_interval3_alt=(70,135]        0
   tipo_de_plan_2_mod=WO residential.time_interval3_alt=(70,135]                 0
                                                                         Status
 Stratum                                                                  Event
   tipo_de_plan_2_mod=basic ambulatory.time_interval3_alt=[0,10]            313
   tipo_de_plan_2_mod=GP intensive ambulatory.time_interval3_alt=[0,10]     427
   tipo_de_plan_2_mod=GP residential.time_interval3_alt=[0,10]              306
   tipo_de_plan_2_mod=WO intensive ambulatory.time_interval3_alt=[0,10]      55
   tipo_de_plan_2_mod=WO residential.time_interval3_alt=[0,10]              178
   tipo_de_plan_2_mod=basic ambulatory.time_interval3_alt=(10,20]          1040
   tipo_de_plan_2_mod=GP intensive ambulatory.time_interval3_alt=(10,20]   1288
   tipo_de_plan_2_mod=GP residential.time_interval3_alt=(10,20]             673
   tipo_de_plan_2_mod=WO intensive ambulatory.time_interval3_alt=(10,20]    223
   tipo_de_plan_2_mod=WO residential.time_interval3_alt=(10,20]             322
   tipo_de_plan_2_mod=basic ambulatory.time_interval3_alt=(20,30]          1073
   tipo_de_plan_2_mod=GP intensive ambulatory.time_interval3_alt=(20,30]   1311
   tipo_de_plan_2_mod=GP residential.time_interval3_alt=(20,30]             578
   tipo_de_plan_2_mod=WO intensive ambulatory.time_interval3_alt=(20,30]    216
   tipo_de_plan_2_mod=WO residential.time_interval3_alt=(20,30]             310
   tipo_de_plan_2_mod=basic ambulatory.time_interval3_alt=(30,50]          1535
   tipo_de_plan_2_mod=GP intensive ambulatory.time_interval3_alt=(30,50]   1774
   tipo_de_plan_2_mod=GP residential.time_interval3_alt=(30,50]             752
   tipo_de_plan_2_mod=WO intensive ambulatory.time_interval3_alt=(30,50]    265
   tipo_de_plan_2_mod=WO residential.time_interval3_alt=(30,50]             370
   tipo_de_plan_2_mod=basic ambulatory.time_interval3_alt=(50,70]           973
   tipo_de_plan_2_mod=GP intensive ambulatory.time_interval3_alt=(50,70]    952
   tipo_de_plan_2_mod=GP residential.time_interval3_alt=(50,70]             392
   tipo_de_plan_2_mod=WO intensive ambulatory.time_interval3_alt=(50,70]    147
   tipo_de_plan_2_mod=WO residential.time_interval3_alt=(50,70]             178
   tipo_de_plan_2_mod=basic ambulatory.time_interval3_alt=(70,135]          699
   tipo_de_plan_2_mod=GP intensive ambulatory.time_interval3_alt=(70,135]   747
   tipo_de_plan_2_mod=GP residential.time_interval3_alt=(70,135]            325
   tipo_de_plan_2_mod=WO intensive ambulatory.time_interval3_alt=(70,135]   109
   tipo_de_plan_2_mod=WO residential.time_interval3_alt=(70,135]            140
 
                       Model Tests       Discrimination    
                                                Indexes    
 Obs    17671    LR chi2    263.53       R2       0.015    
 Events 17671    d.f.           20    R2(20,17671)0.014    
 Center   105    Pr(> chi2) 0.0000       Dxy      0.251    
                 Score chi2 263.20                         
                 Pr(> chi2) 0.0000                         
 
                               Coef    S.E.   Wald Z Pr(>|Z|)
 lag_tr_outcome                 0.0448 0.0190  2.36  0.0183  
 lag_comp_bpsc_y3_severe        0.0238 0.0170  1.40  0.1629  
 lag_less_90d_tr1               0.0374 0.0227  1.65  0.0993  
 log_lag_dias_treat_imp_sin_na -0.0289 0.0084 -3.44  0.0006  
 lag_policonsumo2              -0.0071 0.0201 -0.35  0.7234  
 edad_al_ing_1                  0.0520 0.0038 13.68  <0.0001 
 ano_nac_corr                   0.0524 0.0038 13.96  <0.0001 
 susinidum_oh                  -0.0306 0.0524 -0.58  0.5591  
 susinidum_coc                  0.0607 0.0617  0.98  0.3256  
 susinidum_pbc                 -0.0096 0.0573 -0.17  0.8671  
 susinidum_mar                 -0.0109 0.0529 -0.21  0.8361  
 psycom_dum_study               0.0047 0.0220  0.21  0.8311  
 psycom_dum_with               -0.0208 0.0169 -1.23  0.2190  
 freq_cons_dum_5day             0.0204 0.0161  1.26  0.2067  
 cond_oc_dum_2inact            -0.0145 0.0222 -0.65  0.5137  
 cond_oc_dum_3unemp             0.0231 0.0178  1.30  0.1938  
 susprindum_oh                  0.0811 0.0694  1.17  0.2423  
 susprindum_coc                 0.0585 0.0700  0.84  0.4034  
 susprindum_pbc                 0.0645 0.0684  0.94  0.3454  
 susprindum_mar                 0.0681 0.0745  0.91  0.3607  
 
Code
# Cox Proportional Hazards Model
# 
# cph(formula = Surv(lag_time, time, event) ~ cluster(id) + lag_tr_outcome + 
#     lag_comp_bpsc_y3_severe + lag_less_90d_tr1 + log_lag_dias_treat_imp_sin_na + 
#     lag_policonsumo2 + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
#     susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_study + 
#     psycom_dum_with + freq_cons_dum_5day + cond_oc_dum_2inact + 
#     cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
#     susprindum_mar + strat(tipo_de_plan_2_mod) + strat(time_interval3_alt), 
#     data = data_mine_miss_restr_proc2 %>% data.table::as.data.table() %>% 
#         data.frame(), x = TRUE, y = TRUE, iter.max = 250 * 4, 
#     tol = 1e-06, surv = TRUE)
# 
# 
#                                                                         Status
# Stratum                                                                  No Event Event
#   tipo_de_plan_2_mod=basic ambulatory.time_interval3_alt=[0,10]                 0   313
#   tipo_de_plan_2_mod=GP intensive ambulatory.time_interval3_alt=[0,10]          0   427
#   tipo_de_plan_2_mod=GP residential.time_interval3_alt=[0,10]                   0   306
#   tipo_de_plan_2_mod=WO intensive ambulatory.time_interval3_alt=[0,10]          0    55
#   tipo_de_plan_2_mod=WO residential.time_interval3_alt=[0,10]                   0   178
#   tipo_de_plan_2_mod=basic ambulatory.time_interval3_alt=(10,20]                0  1040
#   tipo_de_plan_2_mod=GP intensive ambulatory.time_interval3_alt=(10,20]         0  1288
#   tipo_de_plan_2_mod=GP residential.time_interval3_alt=(10,20]                  0   673
#   tipo_de_plan_2_mod=WO intensive ambulatory.time_interval3_alt=(10,20]         0   223
#   tipo_de_plan_2_mod=WO residential.time_interval3_alt=(10,20]                  0   322
#   tipo_de_plan_2_mod=basic ambulatory.time_interval3_alt=(20,30]                0  1073
#   tipo_de_plan_2_mod=GP intensive ambulatory.time_interval3_alt=(20,30]         0  1311
#   tipo_de_plan_2_mod=GP residential.time_interval3_alt=(20,30]                  0   578
#   tipo_de_plan_2_mod=WO intensive ambulatory.time_interval3_alt=(20,30]         0   216
#   tipo_de_plan_2_mod=WO residential.time_interval3_alt=(20,30]                  0   310
#   tipo_de_plan_2_mod=basic ambulatory.time_interval3_alt=(30,50]                0  1535
#   tipo_de_plan_2_mod=GP intensive ambulatory.time_interval3_alt=(30,50]         0  1774
#   tipo_de_plan_2_mod=GP residential.time_interval3_alt=(30,50]                  0   752
#   tipo_de_plan_2_mod=WO intensive ambulatory.time_interval3_alt=(30,50]         0   265
#   tipo_de_plan_2_mod=WO residential.time_interval3_alt=(30,50]                  0   370
#   tipo_de_plan_2_mod=basic ambulatory.time_interval3_alt=(50,70]                0   973
#   tipo_de_plan_2_mod=GP intensive ambulatory.time_interval3_alt=(50,70]         0   952
#   tipo_de_plan_2_mod=GP residential.time_interval3_alt=(50,70]                  0   392
#   tipo_de_plan_2_mod=WO intensive ambulatory.time_interval3_alt=(50,70]         0   147
#   tipo_de_plan_2_mod=WO residential.time_interval3_alt=(50,70]                  0   178
#   tipo_de_plan_2_mod=basic ambulatory.time_interval3_alt=(70,135]               0   699
#   tipo_de_plan_2_mod=GP intensive ambulatory.time_interval3_alt=(70,135]        0   747
#   tipo_de_plan_2_mod=GP residential.time_interval3_alt=(70,135]                 0   325
#   tipo_de_plan_2_mod=WO intensive ambulatory.time_interval3_alt=(70,135]        0   109
#   tipo_de_plan_2_mod=WO residential.time_interval3_alt=(70,135]                 0   140
# 
#                       Model Tests       Discrimination    
#                                                Indexes    
# Obs    17671    LR chi2    263.53       R2       0.015    
# Events 17671    d.f.           20    R2(20,17671)0.014    
# Center   105    Pr(> chi2) 0.0000       Dxy      0.251    
#                 Score chi2 263.20                         
#                 Pr(> chi2) 0.0000                         
# 
#                               Coef    S.E.   Wald Z Pr(>|Z|)
# lag_tr_outcome                 0.0448 0.0190  2.36  0.0183  
# lag_comp_bpsc_y3_severe        0.0238 0.0170  1.40  0.1629  
# lag_less_90d_tr1               0.0374 0.0227  1.65  0.0993  
# log_lag_dias_treat_imp_sin_na -0.0289 0.0084 -3.44  0.0006  
# lag_policonsumo2              -0.0071 0.0201 -0.35  0.7234  
# edad_al_ing_1                  0.0520 0.0038 13.68  <0.0001 
# ano_nac_corr                   0.0524 0.0038 13.96  <0.0001 
# susinidum_oh                  -0.0306 0.0524 -0.58  0.5591  
# susinidum_coc                  0.0607 0.0617  0.98  0.3256  
# susinidum_pbc                 -0.0096 0.0573 -0.17  0.8671  
# susinidum_mar                 -0.0109 0.0529 -0.21  0.8361  
# psycom_dum_study               0.0047 0.0220  0.21  0.8311  
# psycom_dum_with               -0.0208 0.0169 -1.23  0.2190  
# freq_cons_dum_5day             0.0204 0.0161  1.26  0.2067  
# cond_oc_dum_2inact            -0.0145 0.0222 -0.65  0.5137  
# cond_oc_dum_3unemp             0.0231 0.0178  1.30  0.1938  
# susprindum_oh                  0.0811 0.0694  1.17  0.2423  
# susprindum_coc                 0.0585 0.0700  0.84  0.4034  
# susprindum_pbc                 0.0645 0.0684  0.94  0.3454  
# susprindum_mar                 0.0681 0.0745  0.91  0.3607 

cox.zph(model_after_time_strat_alt)
                                 chisq df       p
lag_tr_outcome                  6.8110  1  0.0091
lag_comp_bpsc_y3_severe         3.2478  1  0.0715
lag_less_90d_tr1               16.6116  1 4.6e-05
log_lag_dias_treat_imp_sin_na  32.2194  1 1.4e-08
lag_policonsumo2                0.6498  1  0.4202
edad_al_ing_1                   3.4324  1  0.0639
ano_nac_corr                    0.2407  1  0.6237
susinidum_oh                    3.3813  1  0.0659
susinidum_coc                   1.5745  1  0.2096
susinidum_pbc                   1.7148  1  0.1904
susinidum_mar                   0.5145  1  0.4732
psycom_dum_study                5.8375  1  0.0157
psycom_dum_with                 3.0765  1  0.0794
freq_cons_dum_5day              0.2376  1  0.6259
cond_oc_dum_2inact              0.7335  1  0.3918
cond_oc_dum_3unemp              4.4843  1  0.0342
susprindum_oh                   4.5222  1  0.0335
susprindum_coc                  0.0816  1  0.7751
susprindum_pbc                  2.2638  1  0.1324
susprindum_mar                  1.6041  1  0.2053
GLOBAL                        177.1396 20 < 2e-16
Code
#                                  chisq df       p
# lag_tr_outcome                  6.8110  1  0.0091
# lag_comp_bpsc_y3_severe         3.2478  1  0.0715
# lag_less_90d_tr1               16.6116  1 4.6e-05
# log_lag_dias_treat_imp_sin_na  32.2194  1 1.4e-08
# lag_policonsumo2                0.6498  1  0.4202
# edad_al_ing_1                   3.4324  1  0.0639
# ano_nac_corr                    0.2407  1  0.6237
# susinidum_oh                    3.3813  1  0.0659
# susinidum_coc                   1.5745  1  0.2096
# susinidum_pbc                   1.7148  1  0.1904
# susinidum_mar                   0.5145  1  0.4732
# psycom_dum_study                5.8375  1  0.0157
# psycom_dum_with                 3.0765  1  0.0794
# freq_cons_dum_5day              0.2376  1  0.6259
# cond_oc_dum_2inact              0.7335  1  0.3918
# cond_oc_dum_3unemp              4.4843  1  0.0342
# susprindum_oh                   4.5222  1  0.0335
# susprindum_coc                  0.0816  1  0.7751
# susprindum_pbc                  2.2638  1  0.1324
# susprindum_mar                  1.6041  1  0.2053
# GLOBAL                        177.1396 20 < 2e-16

table(data_mine_miss_restr_proc2$time_interval3_alt, data_mine_miss_restr_proc2$tipo_de_plan_2_mod)
          
           basic ambulatory GP intensive ambulatory GP residential
  [0,10]               3756                    4289           2015
  (10,20]              1787                    2228           1104
  (20,30]              1208                    1468            612
  (30,50]              1568                    1813            756
  (50,70]               975                     952            392
  (70,135]              699                     747            325
          
           WO intensive ambulatory WO residential
  [0,10]                       602            994
  (10,20]                      383            510
  (20,30]                      247            337
  (30,50]                      272            374
  (50,70]                      147            179
  (70,135]                     109            140
Code
#          basic ambulatory GP intensive ambulatory GP residential WO intensive ambulatory WO residential
# [0,10]               3756                    4289           2015                     602            994
# (10,20]              1787                    2228           1104                     383            510
# (20,30]              1208                    1468            612                     247            337
# (30,50]              1568                    1813            756                     272            374
# (50,70]               975                     952            392                     147            179
# (70,135]              699                     747            325                     109            140
Code
folder_path <- ifelse(dir.exists("E:/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/"),
                      "E:/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/",
                      "C:/Users/CISS Fondecyt/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/")
save.image(paste0(folder_path,"20240503_avance_iiw.RData"))
Apply
Code
#iiws_strat_id <- data.frame(spec = character(), aic = numeric(), cox_zph = numeric(), success = logical())

iiws_strat <- list()

comb_strata <- expand.grid(unique(data_mine_miss_restr_proc2$tipo_de_plan_2_mod), 
                           unique(data_mine_miss_restr_proc2$time_interval3))

for (xx in seq_along(1:nrow(comb_strata))) {
  i<- comb_strata[xx,"Var1"]
  j<- comb_strata[xx,"Var2"]
  data_mine_miss_proc2_subset<-
  subset(data_mine_miss_restr_proc2, subset= tipo_de_plan_2_mod==i & time_interval3==j)
  maxfu_df_subset <- subset(maxfu_restr_df, subset= maxfu.id %in% unique(data_mine_miss_proc2_subset$id))
  iiw_model_strat<-
    IrregLong::iiw.weights(Surv(time.lag,time,event)~ 
          cluster(id)+ #If a frailty model is used, the cluster(id) term should appear before other covariates
          tr_outcome.lag+
          less_90d_tr1.lag+
          log_dias_treat_imp_sin_na.lag +
          comp_bpsc_y3_severe.lag+
          policonsumo2.lag + 
          edad_al_ing_1 + 
          ano_nac_corr + 
          susinidum_oh +
          susinidum_coc +
          susinidum_pbc +
          susinidum_mar +
          psycom_dum_with +
          psycom_dum_study + 
          freq_cons_dum_5day +
          cond_oc_dum_2inact +
          cond_oc_dum_3unemp +
          susprindum_oh +
          susprindum_coc +
          susprindum_pbc +
          susprindum_mar, 
      data= data_mine_miss_proc2_subset,
      id= "id",
      time= "time",
      event= "event", #character string indicating which column of the data indicates whether or not a visit occurred. If every row corresponds to a visit, then this column will consist entirely of ones
      maxfu= maxfu_df_subset,
      invariant= c("edad_al_ing_1", "ano_nac_corr", "susinidum_oh", "susinidum_coc", "susinidum_pbc", "susinidum_mar",  "psycom_dum_with", "psycom_dum_study_rec2", "freq_cons_dum_5day", "cond_oc_dum_3unemp", "cond_oc_dum_2inact", "susprindum_oh", "susprindum_coc", "susprindum_pbc", "susprindum_mar"),
      lagvars= c("time", "tr_outcome","log_dias_treat_imp_sin_na", "less_90d_tr1","comp_bpsc_y3_severe", "policonsumo2"),
      lagfirst= c(2.95082,0,4.499811/2,0,0,0),  #90/30.5 4.499811 es 90 días
      first= T
)
  
  iiws_strat[[xx]] <- list(
      model = iiw_model_strat,
      comb = paste0("Modality: ",i,"\nTime interval: ", j,"."),
      cox_iiw = summary(iiw_model_strat$m),
      iiw = summary(iiw_model_strat$iiw)
  )
  print(paste0("Modality: ",i,"\nTime interval: ", j))
  print(summary(iiw_model_strat$m))
  print(summary(iiw_model_strat$iiw))

}
[1] "Modality: WO residential\nTime interval: [0,88]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 2106, number of events= 2106 
   (1417 observations deleted due to missingness)

                                  coef exp(coef) se(coef) robust se     z
tr_outcome.lag                -0.19320   0.82432  0.05878   0.05420 -3.56
less_90d_tr1.lag              -0.20058   0.81825  0.06876   0.08209 -2.44
log_dias_treat_imp_sin_na.lag -0.14656   0.86367  0.01558   0.02449 -5.98
comp_bpsc_y3_severe.lag       -0.26031   0.77081  0.05617   0.06067 -4.29
policonsumo2.lag              -0.43585   0.64671  0.06064   0.06111 -7.13
edad_al_ing_1                  0.11002   1.11630  0.01083   0.00926 11.89
ano_nac_corr                   0.12040   1.12795  0.01059   0.00897 13.42
susinidum_oh                   0.21004   1.23372  0.16479   0.13855  1.52
susinidum_coc                  0.27717   1.31939  0.18873   0.15659  1.77
susinidum_pbc                  0.21194   1.23607  0.17095   0.14735  1.44
susinidum_mar                  0.26304   1.30088  0.16570   0.14167  1.86
psycom_dum_with                0.07609   1.07906  0.06264   0.05653  1.35
psycom_dum_study              -0.04540   0.95561  0.08076   0.07887 -0.58
freq_cons_dum_5day            -0.00615   0.99387  0.05188   0.04702 -0.13
cond_oc_dum_2inact             0.02411   1.02441  0.08128   0.08206  0.29
cond_oc_dum_3unemp             0.11695   1.12406  0.08213   0.08238  1.42
susprindum_oh                  0.10001   1.10518  0.15112   0.13956  0.72
susprindum_coc                -0.00818   0.99185  0.15385   0.14136 -0.06
susprindum_pbc                 0.08569   1.08947  0.14360   0.13471  0.64
susprindum_mar                -0.03637   0.96429  0.21621   0.18971 -0.19
                              Pr(>|z|)    
tr_outcome.lag                 0.00036 ***
less_90d_tr1.lag               0.01455 *  
log_dias_treat_imp_sin_na.lag  2.2e-09 ***
comp_bpsc_y3_severe.lag        1.8e-05 ***
policonsumo2.lag               9.9e-13 ***
edad_al_ing_1                  < 2e-16 ***
ano_nac_corr                   < 2e-16 ***
susinidum_oh                   0.12952    
susinidum_coc                  0.07673 .  
susinidum_pbc                  0.15034    
susinidum_mar                  0.06334 .  
psycom_dum_with                0.17828    
psycom_dum_study               0.56488    
freq_cons_dum_5day             0.89601    
cond_oc_dum_2inact             0.76885    
cond_oc_dum_3unemp             0.15573    
susprindum_oh                  0.47362    
susprindum_coc                 0.95386    
susprindum_pbc                 0.52469    
susprindum_mar                 0.84798    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    0.824      1.213     0.741     0.917
less_90d_tr1.lag                  0.818      1.222     0.697     0.961
log_dias_treat_imp_sin_na.lag     0.864      1.158     0.823     0.906
comp_bpsc_y3_severe.lag           0.771      1.297     0.684     0.868
policonsumo2.lag                  0.647      1.546     0.574     0.729
edad_al_ing_1                     1.116      0.896     1.096     1.137
ano_nac_corr                      1.128      0.887     1.108     1.148
susinidum_oh                      1.234      0.811     0.940     1.619
susinidum_coc                     1.319      0.758     0.971     1.793
susinidum_pbc                     1.236      0.809     0.926     1.650
susinidum_mar                     1.301      0.769     0.985     1.717
psycom_dum_with                   1.079      0.927     0.966     1.205
psycom_dum_study                  0.956      1.046     0.819     1.115
freq_cons_dum_5day                0.994      1.006     0.906     1.090
cond_oc_dum_2inact                1.024      0.976     0.872     1.203
cond_oc_dum_3unemp                1.124      0.890     0.956     1.321
susprindum_oh                     1.105      0.905     0.841     1.453
susprindum_coc                    0.992      1.008     0.752     1.308
susprindum_pbc                    1.089      0.918     0.837     1.419
susprindum_mar                    0.964      1.037     0.665     1.399

Concordance= 0.653  (se = 0.005 )
Likelihood ratio test= 529  on 20 df,   p=<2e-16
Wald test            = 611  on 20 df,   p=<2e-16
Score (logrank) test = 555  on 20 df,   p=<2e-16,   Robust = 539  p=<2e-16

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.20    0.58    1.09    1.28    1.87    4.98 
[1] "Modality: GP intensive ambulatory\nTime interval: [0,88]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 9973, number of events= 9973 
   (6233 observations deleted due to missingness)

                                  coef exp(coef) se(coef) robust se      z
tr_outcome.lag                -0.41578   0.65982  0.02667   0.03341 -12.44
less_90d_tr1.lag              -0.21477   0.80673  0.03249   0.04867  -4.41
log_dias_treat_imp_sin_na.lag -0.21470   0.80678  0.00596   0.02474  -8.68
comp_bpsc_y3_severe.lag       -0.17022   0.84348  0.02524   0.02689  -6.33
policonsumo2.lag              -0.50518   0.60340  0.02670   0.03880 -13.02
edad_al_ing_1                  0.12951   1.13827  0.00497   0.00468  27.69
ano_nac_corr                   0.13571   1.14535  0.00490   0.00455  29.85
susinidum_oh                  -0.02056   0.97965  0.06953   0.06257  -0.33
susinidum_coc                  0.01638   1.01652  0.08325   0.07644   0.21
susinidum_pbc                  0.06987   1.07237  0.07878   0.07419   0.94
susinidum_mar                  0.08715   1.09106  0.07084   0.06432   1.35
psycom_dum_with                0.03784   1.03857  0.02222   0.02392   1.58
psycom_dum_study               0.04332   1.04427  0.03479   0.04956   0.87
freq_cons_dum_5day             0.07887   1.08207  0.02063   0.02328   3.39
cond_oc_dum_2inact            -0.00544   0.99458  0.03099   0.03969  -0.14
cond_oc_dum_3unemp             0.03677   1.03746  0.02222   0.02389   1.54
susprindum_oh                 -0.03507   0.96554  0.09009   0.10089  -0.35
susprindum_coc                 0.06420   1.06631  0.09125   0.10167   0.63
susprindum_pbc                 0.11017   1.11647  0.08896   0.10047   1.10
susprindum_mar                 0.00978   1.00983  0.09767   0.11394   0.09
                              Pr(>|z|)    
tr_outcome.lag                 < 2e-16 ***
less_90d_tr1.lag               1.0e-05 ***
log_dias_treat_imp_sin_na.lag  < 2e-16 ***
comp_bpsc_y3_severe.lag        2.4e-10 ***
policonsumo2.lag               < 2e-16 ***
edad_al_ing_1                  < 2e-16 ***
ano_nac_corr                   < 2e-16 ***
susinidum_oh                    0.7424    
susinidum_coc                   0.8303    
susinidum_pbc                   0.3463    
susinidum_mar                   0.1755    
psycom_dum_with                 0.1136    
psycom_dum_study                0.3820    
freq_cons_dum_5day              0.0007 ***
cond_oc_dum_2inact              0.8910    
cond_oc_dum_3unemp              0.1238    
susprindum_oh                   0.7281    
susprindum_coc                  0.5277    
susprindum_pbc                  0.2728    
susprindum_mar                  0.9316    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    0.660      1.516     0.618     0.704
less_90d_tr1.lag                  0.807      1.240     0.733     0.887
log_dias_treat_imp_sin_na.lag     0.807      1.239     0.769     0.847
comp_bpsc_y3_severe.lag           0.843      1.186     0.800     0.889
policonsumo2.lag                  0.603      1.657     0.559     0.651
edad_al_ing_1                     1.138      0.879     1.128     1.149
ano_nac_corr                      1.145      0.873     1.135     1.156
susinidum_oh                      0.980      1.021     0.867     1.107
susinidum_coc                     1.017      0.984     0.875     1.181
susinidum_pbc                     1.072      0.933     0.927     1.240
susinidum_mar                     1.091      0.917     0.962     1.238
psycom_dum_with                   1.039      0.963     0.991     1.088
psycom_dum_study                  1.044      0.958     0.948     1.151
freq_cons_dum_5day                1.082      0.924     1.034     1.133
cond_oc_dum_2inact                0.995      1.005     0.920     1.075
cond_oc_dum_3unemp                1.037      0.964     0.990     1.087
susprindum_oh                     0.966      1.036     0.792     1.177
susprindum_coc                    1.066      0.938     0.874     1.301
susprindum_pbc                    1.116      0.896     0.917     1.359
susprindum_mar                    1.010      0.990     0.808     1.263

Concordance= 0.688  (se = 0.002 )
Likelihood ratio test= 3666  on 20 df,   p=<2e-16
Wald test            = 3057  on 20 df,   p=<2e-16
Score (logrank) test = 4025  on 20 df,   p=<2e-16,   Robust = 3204  p=<2e-16

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.19    0.62    1.39    1.85    2.82    9.29 
[1] "Modality: basic ambulatory\nTime interval: [0,88]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 8836, number of events= 8836 
   (5238 observations deleted due to missingness)

                                  coef exp(coef) se(coef) robust se      z
tr_outcome.lag                -0.49853   0.60743  0.02786   0.04278 -11.65
less_90d_tr1.lag              -0.29625   0.74360  0.03653   0.05216  -5.68
log_dias_treat_imp_sin_na.lag -0.26775   0.76510  0.00666   0.03198  -8.37
comp_bpsc_y3_severe.lag        0.03051   1.03098  0.03630   0.03660   0.83
policonsumo2.lag              -0.50815   0.60161  0.02767   0.04248 -11.96
edad_al_ing_1                  0.12296   1.13084  0.00541   0.00517  23.78
ano_nac_corr                   0.13106   1.14004  0.00535   0.00523  25.04
susinidum_oh                   0.03928   1.04007  0.07640   0.06470   0.61
susinidum_coc                  0.10149   1.10682  0.09014   0.08332   1.22
susinidum_pbc                  0.16419   1.17844  0.08742   0.07761   2.12
susinidum_mar                  0.13452   1.14399  0.07752   0.06679   2.01
psycom_dum_with               -0.00807   0.99196  0.02372   0.02648  -0.30
psycom_dum_study               0.17443   1.19057  0.03412   0.04334   4.03
freq_cons_dum_5day             0.00245   1.00245  0.02458   0.02389   0.10
cond_oc_dum_2inact             0.03825   1.03899  0.03378   0.03151   1.21
cond_oc_dum_3unemp             0.06070   1.06258  0.02600   0.02462   2.47
susprindum_oh                  0.03566   1.03631  0.10103   0.07942   0.45
susprindum_coc                 0.05788   1.05959  0.10247   0.08154   0.71
susprindum_pbc                 0.13649   1.14625  0.10072   0.08066   1.69
susprindum_mar                 0.10793   1.11397  0.10699   0.08789   1.23
                              Pr(>|z|)    
tr_outcome.lag                 < 2e-16 ***
less_90d_tr1.lag               1.4e-08 ***
log_dias_treat_imp_sin_na.lag  < 2e-16 ***
comp_bpsc_y3_severe.lag          0.404    
policonsumo2.lag               < 2e-16 ***
edad_al_ing_1                  < 2e-16 ***
ano_nac_corr                   < 2e-16 ***
susinidum_oh                     0.544    
susinidum_coc                    0.223    
susinidum_pbc                    0.034 *  
susinidum_mar                    0.044 *  
psycom_dum_with                  0.760    
psycom_dum_study               5.7e-05 ***
freq_cons_dum_5day               0.918    
cond_oc_dum_2inact               0.225    
cond_oc_dum_3unemp               0.014 *  
susprindum_oh                    0.653    
susprindum_coc                   0.478    
susprindum_pbc                   0.091 .  
susprindum_mar                   0.219    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    0.607      1.646     0.559     0.661
less_90d_tr1.lag                  0.744      1.345     0.671     0.824
log_dias_treat_imp_sin_na.lag     0.765      1.307     0.719     0.815
comp_bpsc_y3_severe.lag           1.031      0.970     0.960     1.108
policonsumo2.lag                  0.602      1.662     0.554     0.654
edad_al_ing_1                     1.131      0.884     1.119     1.142
ano_nac_corr                      1.140      0.877     1.128     1.152
susinidum_oh                      1.040      0.961     0.916     1.181
susinidum_coc                     1.107      0.903     0.940     1.303
susinidum_pbc                     1.178      0.849     1.012     1.372
susinidum_mar                     1.144      0.874     1.004     1.304
psycom_dum_with                   0.992      1.008     0.942     1.045
psycom_dum_study                  1.191      0.840     1.094     1.296
freq_cons_dum_5day                1.002      0.998     0.957     1.051
cond_oc_dum_2inact                1.039      0.962     0.977     1.105
cond_oc_dum_3unemp                1.063      0.941     1.013     1.115
susprindum_oh                     1.036      0.965     0.887     1.211
susprindum_coc                    1.060      0.944     0.903     1.243
susprindum_pbc                    1.146      0.872     0.979     1.343
susprindum_mar                    1.114      0.898     0.938     1.323

Concordance= 0.692  (se = 0.003 )
Likelihood ratio test= 3513  on 20 df,   p=<2e-16
Wald test            = 2853  on 20 df,   p=<2e-16
Score (logrank) test = 3910  on 20 df,   p=<2e-16,   Robust = 3228  p=<2e-16

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.11    0.54    1.34    1.80    2.78   11.57 
[1] "Modality: GP residential\nTime interval: [0,88]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 4436, number of events= 4436 
   (2815 observations deleted due to missingness)

                                  coef exp(coef) se(coef) robust se      z
tr_outcome.lag                -0.16991   0.84374  0.03876   0.03958  -4.29
less_90d_tr1.lag              -0.19179   0.82548  0.04678   0.05353  -3.58
log_dias_treat_imp_sin_na.lag -0.08727   0.91643  0.00829   0.01870  -4.67
comp_bpsc_y3_severe.lag       -0.24899   0.77959  0.03621   0.04101  -6.07
policonsumo2.lag              -0.60356   0.54686  0.03971   0.04967 -12.15
edad_al_ing_1                  0.09289   1.09734  0.00765   0.00648  14.34
ano_nac_corr                   0.09753   1.10245  0.00760   0.00638  15.28
susinidum_oh                   0.11062   1.11697  0.10673   0.12937   0.86
susinidum_coc                  0.17321   1.18912  0.13021   0.14678   1.18
susinidum_pbc                  0.17984   1.19703  0.11357   0.13542   1.33
susinidum_mar                  0.29456   1.34254  0.10690   0.12899   2.28
psycom_dum_with               -0.04422   0.95675  0.03443   0.03172  -1.39
psycom_dum_study               0.01251   1.01259  0.05082   0.05272   0.24
freq_cons_dum_5day             0.08040   1.08372  0.03485   0.03422   2.35
cond_oc_dum_2inact             0.09636   1.10116  0.05613   0.05494   1.75
cond_oc_dum_3unemp             0.19515   1.21549  0.04615   0.04405   4.43
susprindum_oh                 -0.11367   0.89255  0.16054   0.14683  -0.77
susprindum_coc                -0.02726   0.97311  0.16143   0.14574  -0.19
susprindum_pbc                -0.04708   0.95401  0.15660   0.14174  -0.33
susprindum_mar                -0.16196   0.85048  0.18174   0.18838  -0.86
                              Pr(>|z|)    
tr_outcome.lag                 1.8e-05 ***
less_90d_tr1.lag               0.00034 ***
log_dias_treat_imp_sin_na.lag  3.1e-06 ***
comp_bpsc_y3_severe.lag        1.3e-09 ***
policonsumo2.lag               < 2e-16 ***
edad_al_ing_1                  < 2e-16 ***
ano_nac_corr                   < 2e-16 ***
susinidum_oh                   0.39250    
susinidum_coc                  0.23796    
susinidum_pbc                  0.18418    
susinidum_mar                  0.02239 *  
psycom_dum_with                0.16332    
psycom_dum_study               0.81244    
freq_cons_dum_5day             0.01878 *  
cond_oc_dum_2inact             0.07944 .  
cond_oc_dum_3unemp             9.4e-06 ***
susprindum_oh                  0.43884    
susprindum_coc                 0.85165    
susprindum_pbc                 0.73975    
susprindum_mar                 0.38994    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    0.844      1.185     0.781     0.912
less_90d_tr1.lag                  0.825      1.211     0.743     0.917
log_dias_treat_imp_sin_na.lag     0.916      1.091     0.883     0.951
comp_bpsc_y3_severe.lag           0.780      1.283     0.719     0.845
policonsumo2.lag                  0.547      1.829     0.496     0.603
edad_al_ing_1                     1.097      0.911     1.084     1.111
ano_nac_corr                      1.102      0.907     1.089     1.116
susinidum_oh                      1.117      0.895     0.867     1.439
susinidum_coc                     1.189      0.841     0.892     1.586
susinidum_pbc                     1.197      0.835     0.918     1.561
susinidum_mar                     1.343      0.745     1.043     1.729
psycom_dum_with                   0.957      1.045     0.899     1.018
psycom_dum_study                  1.013      0.988     0.913     1.123
freq_cons_dum_5day                1.084      0.923     1.013     1.159
cond_oc_dum_2inact                1.101      0.908     0.989     1.226
cond_oc_dum_3unemp                1.215      0.823     1.115     1.325
susprindum_oh                     0.893      1.120     0.669     1.190
susprindum_coc                    0.973      1.028     0.731     1.295
susprindum_pbc                    0.954      1.048     0.723     1.260
susprindum_mar                    0.850      1.176     0.588     1.230

Concordance= 0.648  (se = 0.004 )
Likelihood ratio test= 1017  on 20 df,   p=<2e-16
Wald test            = 1099  on 20 df,   p=<2e-16
Score (logrank) test = 1052  on 20 df,   p=<2e-16,   Robust = 998  p=<2e-16

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.27    0.66    1.22    1.42    2.07    4.40 
[1] "Modality: WO intensive ambulatory\nTime interval: [0,88]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 1546, number of events= 1546 
   (908 observations deleted due to missingness)

                                 coef exp(coef) se(coef) robust se      z
tr_outcome.lag                -0.1752    0.8393   0.0737    0.0693  -2.53
less_90d_tr1.lag              -0.3547    0.7014   0.0956    0.0974  -3.64
log_dias_treat_imp_sin_na.lag -0.4430    0.6421   0.0288    0.0285 -15.52
comp_bpsc_y3_severe.lag       -0.0625    0.9394   0.0658    0.0629  -0.99
policonsumo2.lag              -0.1927    0.8247   0.0777    0.0715  -2.69
edad_al_ing_1                  0.1324    1.1416   0.0130    0.0117  11.30
ano_nac_corr                   0.1351    1.1447   0.0127    0.0113  11.96
susinidum_oh                  -0.1597    0.8524   0.1803    0.1983  -0.81
susinidum_coc                  0.0881    1.0921   0.2123    0.2232   0.39
susinidum_pbc                 -0.0693    0.9331   0.1926    0.2043  -0.34
susinidum_mar                  0.0105    1.0105   0.1817    0.2002   0.05
psycom_dum_with               -0.0250    0.9753   0.0579    0.0539  -0.46
psycom_dum_study              -0.0806    0.9225   0.0915    0.0945  -0.85
freq_cons_dum_5day             0.0368    1.0375   0.0530    0.0513   0.72
cond_oc_dum_2inact             0.1451    1.1561   0.0667    0.0637   2.28
cond_oc_dum_3unemp             0.0518    1.0531   0.0692    0.0694   0.75
susprindum_oh                 -0.2115    0.8094   0.1931    0.1562  -1.35
susprindum_coc                -0.0335    0.9671   0.1974    0.1628  -0.21
susprindum_pbc                -0.0755    0.9273   0.1908    0.1590  -0.47
susprindum_mar                -0.2156    0.8060   0.2084    0.1865  -1.16
                              Pr(>|z|)    
tr_outcome.lag                 0.01154 *  
less_90d_tr1.lag               0.00027 ***
log_dias_treat_imp_sin_na.lag  < 2e-16 ***
comp_bpsc_y3_severe.lag        0.32051    
policonsumo2.lag               0.00705 ** 
edad_al_ing_1                  < 2e-16 ***
ano_nac_corr                   < 2e-16 ***
susinidum_oh                   0.42062    
susinidum_coc                  0.69305    
susinidum_pbc                  0.73457    
susinidum_mar                  0.95833    
psycom_dum_with                0.64313    
psycom_dum_study               0.39373    
freq_cons_dum_5day             0.47326    
cond_oc_dum_2inact             0.02281 *  
cond_oc_dum_3unemp             0.45561    
susprindum_oh                  0.17571    
susprindum_coc                 0.83715    
susprindum_pbc                 0.63501    
susprindum_mar                 0.24748    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    0.839      1.191     0.733     0.962
less_90d_tr1.lag                  0.701      1.426     0.579     0.849
log_dias_treat_imp_sin_na.lag     0.642      1.557     0.607     0.679
comp_bpsc_y3_severe.lag           0.939      1.064     0.830     1.063
policonsumo2.lag                  0.825      1.213     0.717     0.949
edad_al_ing_1                     1.142      0.876     1.116     1.168
ano_nac_corr                      1.145      0.874     1.120     1.170
susinidum_oh                      0.852      1.173     0.578     1.257
susinidum_coc                     1.092      0.916     0.705     1.691
susinidum_pbc                     0.933      1.072     0.625     1.393
susinidum_mar                     1.011      0.990     0.682     1.496
psycom_dum_with                   0.975      1.025     0.878     1.084
psycom_dum_study                  0.923      1.084     0.766     1.110
freq_cons_dum_5day                1.037      0.964     0.938     1.147
cond_oc_dum_2inact                1.156      0.865     1.020     1.310
cond_oc_dum_3unemp                1.053      0.950     0.919     1.206
susprindum_oh                     0.809      1.236     0.596     1.099
susprindum_coc                    0.967      1.034     0.703     1.331
susprindum_pbc                    0.927      1.078     0.679     1.266
susprindum_mar                    0.806      1.241     0.559     1.162

Concordance= 0.692  (se = 0.006 )
Likelihood ratio test= 626  on 20 df,   p=<2e-16
Wald test            = 626  on 20 df,   p=<2e-16
Score (logrank) test = 659  on 20 df,   p=<2e-16,   Robust = 544  p=<2e-16

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.21    0.54    1.46    1.93    2.89   13.67 
[1] "Modality: WO residential\nTime interval: (93,135]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 34, number of events= 34 
   (32 observations deleted due to missingness)

                                   coef exp(coef)  se(coef) robust se     z
tr_outcome.lag                -1.44e+01  5.81e-07  7.40e+00  6.96e+00 -2.06
less_90d_tr1.lag                     NA        NA  0.00e+00  0.00e+00    NA
log_dias_treat_imp_sin_na.lag  4.42e+00  8.31e+01  2.12e+00  1.90e+00  2.32
comp_bpsc_y3_severe.lag              NA        NA  0.00e+00  0.00e+00    NA
policonsumo2.lag                     NA        NA  0.00e+00  0.00e+00    NA
edad_al_ing_1                  2.49e+00  1.20e+01  7.01e-01  8.29e-01  3.00
ano_nac_corr                   2.37e+00  1.07e+01  6.78e-01  8.04e-01  2.95
susinidum_oh                  -4.33e+00  1.32e-02  1.64e+00  1.38e+00 -3.14
susinidum_coc                        NA        NA  0.00e+00  0.00e+00    NA
susinidum_pbc                 -4.56e+00  1.05e-02  1.69e+00  1.54e+00 -2.96
susinidum_mar                 -4.08e+00  1.69e-02  1.53e+00  1.07e+00 -3.81
psycom_dum_with                3.15e-01  1.37e+00  6.25e-01  5.93e-01  0.53
psycom_dum_study               1.83e+00  6.23e+00  1.23e+00  1.12e+00  1.63
freq_cons_dum_5day            -2.80e-01  7.56e-01  5.60e-01  6.16e-01 -0.45
cond_oc_dum_2inact             6.22e-01  1.86e+00  1.24e+00  1.77e+00  0.35
cond_oc_dum_3unemp             1.16e+00  3.18e+00  1.15e+00  1.69e+00  0.68
susprindum_oh                 -1.67e+00  1.88e-01  1.93e+00  1.73e+00 -0.97
susprindum_coc                -5.05e-01  6.04e-01  1.46e+00  1.09e+00 -0.46
susprindum_pbc                -5.15e-01  5.98e-01  1.21e+00  7.97e-01 -0.65
susprindum_mar                       NA        NA  0.00e+00  0.00e+00    NA
                              Pr(>|z|)    
tr_outcome.lag                 0.03905 *  
less_90d_tr1.lag                    NA    
log_dias_treat_imp_sin_na.lag  0.02013 *  
comp_bpsc_y3_severe.lag             NA    
policonsumo2.lag                    NA    
edad_al_ing_1                  0.00272 ** 
ano_nac_corr                   0.00313 ** 
susinidum_oh                   0.00172 ** 
susinidum_coc                       NA    
susinidum_pbc                  0.00310 ** 
susinidum_mar                  0.00014 ***
psycom_dum_with                0.59557    
psycom_dum_study               0.10206    
freq_cons_dum_5day             0.65023    
cond_oc_dum_2inact             0.72545    
cond_oc_dum_3unemp             0.49430    
susprindum_oh                  0.33431    
susprindum_coc                 0.64295    
susprindum_pbc                 0.51798    
susprindum_mar                      NA    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                 5.81e-07   1.72e+06  6.94e-13     0.486
less_90d_tr1.lag                     NA         NA        NA        NA
log_dias_treat_imp_sin_na.lag  8.31e+01   1.20e-02  2.00e+00  3452.547
comp_bpsc_y3_severe.lag              NA         NA        NA        NA
policonsumo2.lag                     NA         NA        NA        NA
edad_al_ing_1                  1.20e+01   8.33e-02  2.36e+00    60.966
ano_nac_corr                   1.07e+01   9.31e-02  2.22e+00    51.931
susinidum_oh                   1.32e-02   7.59e+01  8.79e-04     0.197
susinidum_coc                        NA         NA        NA        NA
susinidum_pbc                  1.05e-02   9.52e+01  5.13e-04     0.215
susinidum_mar                  1.69e-02   5.92e+01  2.07e-03     0.138
psycom_dum_with                1.37e+00   7.30e-01  4.28e-01     4.384
psycom_dum_study               6.23e+00   1.61e-01  6.95e-01    55.791
freq_cons_dum_5day             7.56e-01   1.32e+00  2.26e-01     2.531
cond_oc_dum_2inact             1.86e+00   5.37e-01  5.78e-02    60.021
cond_oc_dum_3unemp             3.18e+00   3.15e-01  1.15e-01    87.653
susprindum_oh                  1.88e-01   5.31e+00  6.37e-03     5.578
susprindum_coc                 6.04e-01   1.66e+00  7.14e-02     5.101
susprindum_pbc                 5.98e-01   1.67e+00  1.25e-01     2.847
susprindum_mar                       NA         NA        NA        NA

Concordance= 0.777  (se = 0.035 )
Likelihood ratio test= 29.1  on 15 df,   p=0.02
Wald test            = 93.5  on 15 df,   p=2e-13
Score (logrank) test = 36.1  on 15 df,   p=0.002,   Robust = 28.5  p=0.02

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
      3      28      94     179     288     729 
[1] "Modality: GP intensive ambulatory\nTime interval: (93,135]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 213, number of events= 213 
   (202 observations deleted due to missingness)

                                  coef exp(coef) se(coef) robust se     z
tr_outcome.lag                 -0.0698    0.9325   0.7845    0.3906 -0.18
less_90d_tr1.lag                4.6058  100.0624   1.5181    0.7605  6.06
log_dias_treat_imp_sin_na.lag  -0.1856    0.8306   0.2664    0.1478 -1.26
comp_bpsc_y3_severe.lag         0.3339    1.3965   0.6787    0.3763  0.89
policonsumo2.lag               -0.4480    0.6389   0.7853    0.4145 -1.08
edad_al_ing_1                   0.7114    2.0369   0.1093    0.1073  6.63
ano_nac_corr                    0.7070    2.0279   0.1107    0.1092  6.47
susinidum_oh                   -0.0482    0.9530   0.3646    0.3959 -0.12
susinidum_coc                   0.0251    1.0254   0.5269    0.4897  0.05
susinidum_pbc                   0.0582    1.0599   0.5221    0.6476  0.09
susinidum_mar                  -0.0926    0.9116   0.3800    0.4148 -0.22
psycom_dum_with                -0.1165    0.8901   0.1637    0.1548 -0.75
psycom_dum_study               -0.1574    0.8544   0.2574    0.2633 -0.60
freq_cons_dum_5day              0.2429    1.2749   0.1658    0.1569  1.55
cond_oc_dum_2inact             -0.0213    0.9789   0.2682    0.2480 -0.09
cond_oc_dum_3unemp             -0.0428    0.9581   0.1665    0.1641 -0.26
susprindum_oh                  -0.8775    0.4158   0.6748    0.5933 -1.48
susprindum_coc                 -0.9359    0.3922   0.6822    0.6089 -1.54
susprindum_pbc                 -0.5899    0.5544   0.6602    0.5883 -1.00
susprindum_mar                 -0.7845    0.4564   0.7115    0.6598 -1.19
                              Pr(>|z|)    
tr_outcome.lag                    0.86    
less_90d_tr1.lag               1.4e-09 ***
log_dias_treat_imp_sin_na.lag     0.21    
comp_bpsc_y3_severe.lag           0.37    
policonsumo2.lag                  0.28    
edad_al_ing_1                  3.3e-11 ***
ano_nac_corr                   9.6e-11 ***
susinidum_oh                      0.90    
susinidum_coc                     0.96    
susinidum_pbc                     0.93    
susinidum_mar                     0.82    
psycom_dum_with                   0.45    
psycom_dum_study                  0.55    
freq_cons_dum_5day                0.12    
cond_oc_dum_2inact                0.93    
cond_oc_dum_3unemp                0.79    
susprindum_oh                     0.14    
susprindum_coc                    0.12    
susprindum_pbc                    0.32    
susprindum_mar                    0.23    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    0.933    1.07234     0.434      2.01
less_90d_tr1.lag                100.062    0.00999    22.539    444.24
log_dias_treat_imp_sin_na.lag     0.831    1.20397     0.622      1.11
comp_bpsc_y3_severe.lag           1.396    0.71610     0.668      2.92
policonsumo2.lag                  0.639    1.56513     0.284      1.44
edad_al_ing_1                     2.037    0.49094     1.651      2.51
ano_nac_corr                      2.028    0.49313     1.637      2.51
susinidum_oh                      0.953    1.04934     0.439      2.07
susinidum_coc                     1.025    0.97525     0.393      2.68
susinidum_pbc                     1.060    0.94347     0.298      3.77
susinidum_mar                     0.912    1.09702     0.404      2.06
psycom_dum_with                   0.890    1.12353     0.657      1.21
psycom_dum_study                  0.854    1.17043     0.510      1.43
freq_cons_dum_5day                1.275    0.78436     0.937      1.73
cond_oc_dum_2inact                0.979    1.02157     0.602      1.59
cond_oc_dum_3unemp                0.958    1.04374     0.695      1.32
susprindum_oh                     0.416    2.40490     0.130      1.33
susprindum_coc                    0.392    2.54955     0.119      1.29
susprindum_pbc                    0.554    1.80387     0.175      1.76
susprindum_mar                    0.456    2.19123     0.125      1.66

Concordance= 0.65  (se = 0.021 )
Likelihood ratio test= 67.7  on 20 df,   p=4e-07
Wald test            = 141  on 20 df,   p=<2e-16
Score (logrank) test = 79.4  on 20 df,   p=5e-09,   Robust = 61.6  p=4e-06

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.07    1.45    2.09    2.66    3.06   15.52 
[1] "Modality: basic ambulatory\nTime interval: (93,135]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 200, number of events= 200 
   (189 observations deleted due to missingness)

                                  coef exp(coef) se(coef) robust se     z
tr_outcome.lag                 0.06539   1.06758  0.89003   0.52807  0.12
less_90d_tr1.lag                    NA        NA  0.00000   0.00000    NA
log_dias_treat_imp_sin_na.lag -0.80835   0.44559  0.69768   0.41145 -1.96
comp_bpsc_y3_severe.lag        0.68033   1.97453  0.84695   0.44081  1.54
policonsumo2.lag               2.09839   8.15300  1.98344   1.21808  1.72
edad_al_ing_1                  0.76099   2.14038  0.11599   0.12604  6.04
ano_nac_corr                   0.74979   2.11655  0.11568   0.12543  5.98
susinidum_oh                   0.52839   1.69620  0.52634   0.39173  1.35
susinidum_coc                  0.17927   1.19634  0.58825   0.42912  0.42
susinidum_pbc                  0.63893   1.89445  0.57098   0.43414  1.47
susinidum_mar                  0.72591   2.06661  0.53943   0.40960  1.77
psycom_dum_with                0.03583   1.03648  0.17578   0.16452  0.22
psycom_dum_study               0.03533   1.03596  0.21114   0.20776  0.17
freq_cons_dum_5day             0.26250   1.30018  0.16544   0.16890  1.55
cond_oc_dum_2inact             0.27583   1.31763  0.23875   0.28028  0.98
cond_oc_dum_3unemp             0.00744   1.00747  0.18384   0.16345  0.05
susprindum_oh                 -0.71035   0.49147  0.51982   0.46058 -1.54
susprindum_coc                -0.74138   0.47646  0.52322   0.46904 -1.58
susprindum_pbc                -0.64819   0.52299  0.50722   0.44972 -1.44
susprindum_mar                -0.20680   0.81319  0.55075   0.53491 -0.39
                              Pr(>|z|)    
tr_outcome.lag                   0.901    
less_90d_tr1.lag                    NA    
log_dias_treat_imp_sin_na.lag    0.049 *  
comp_bpsc_y3_severe.lag          0.123    
policonsumo2.lag                 0.085 .  
edad_al_ing_1                  1.6e-09 ***
ano_nac_corr                   2.3e-09 ***
susinidum_oh                     0.177    
susinidum_coc                    0.676    
susinidum_pbc                    0.141    
susinidum_mar                    0.076 .  
psycom_dum_with                  0.828    
psycom_dum_study                 0.865    
freq_cons_dum_5day               0.120    
cond_oc_dum_2inact               0.325    
cond_oc_dum_3unemp               0.964    
susprindum_oh                    0.123    
susprindum_coc                   0.114    
susprindum_pbc                   0.149    
susprindum_mar                   0.699    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    1.068      0.937     0.379     3.005
less_90d_tr1.lag                     NA         NA        NA        NA
log_dias_treat_imp_sin_na.lag     0.446      2.244     0.199     0.998
comp_bpsc_y3_severe.lag           1.975      0.506     0.832     4.685
policonsumo2.lag                  8.153      0.123     0.749    88.746
edad_al_ing_1                     2.140      0.467     1.672     2.740
ano_nac_corr                      2.117      0.472     1.655     2.706
susinidum_oh                      1.696      0.590     0.787     3.655
susinidum_coc                     1.196      0.836     0.516     2.774
susinidum_pbc                     1.894      0.528     0.809     4.436
susinidum_mar                     2.067      0.484     0.926     4.612
psycom_dum_with                   1.036      0.965     0.751     1.431
psycom_dum_study                  1.036      0.965     0.689     1.557
freq_cons_dum_5day                1.300      0.769     0.934     1.810
cond_oc_dum_2inact                1.318      0.759     0.761     2.282
cond_oc_dum_3unemp                1.007      0.993     0.731     1.388
susprindum_oh                     0.491      2.035     0.199     1.212
susprindum_coc                    0.476      2.099     0.190     1.195
susprindum_pbc                    0.523      1.912     0.217     1.263
susprindum_mar                    0.813      1.230     0.285     2.320

Concordance= 0.664  (se = 0.022 )
Likelihood ratio test= 66.7  on 19 df,   p=3e-07
Wald test            = 96.4  on 19 df,   p=2e-12
Score (logrank) test = 65.2  on 19 df,   p=6e-07,   Robust = 66.6  p=3e-07

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.16    0.47    0.76    1.07    1.31    7.33 
[1] "Modality: GP residential\nTime interval: (93,135]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 87, number of events= 87 
   (80 observations deleted due to missingness)

                                  coef exp(coef) se(coef) robust se     z
tr_outcome.lag                 0.06200   1.06396  1.61401   1.02888  0.06
less_90d_tr1.lag               1.11330   3.04439  1.21710   0.96928  1.15
log_dias_treat_imp_sin_na.lag  2.21808   9.18970  1.19508   1.12830  1.97
comp_bpsc_y3_severe.lag       -2.81004   0.06020  2.41466   2.01758 -1.39
policonsumo2.lag              -5.68931   0.00338  2.59786   2.19430 -2.59
edad_al_ing_1                  1.01020   2.74615  0.21856   0.19540  5.17
ano_nac_corr                   0.99800   2.71284  0.21846   0.19453  5.13
susinidum_oh                  -1.06298   0.34542  1.05591   0.33960 -3.13
susinidum_coc                 -1.10348   0.33172  1.21863   0.52419 -2.11
susinidum_pbc                 -0.97188   0.37837  1.08524   0.48477 -2.00
susinidum_mar                 -1.19098   0.30392  1.06093   0.32881 -3.62
psycom_dum_with                0.10268   1.10814  0.30116   0.27290  0.38
psycom_dum_study               0.39160   1.47935  0.41112   0.32489  1.21
freq_cons_dum_5day            -0.76394   0.46583  0.29504   0.25756 -2.97
cond_oc_dum_2inact            -0.33948   0.71214  0.50269   0.56349 -0.60
cond_oc_dum_3unemp            -0.73718   0.47846  0.41746   0.52470 -1.40
susprindum_oh                 -3.54379   0.02890  1.29324   0.70176 -5.05
susprindum_coc                -2.46160   0.08530  1.29604   0.79542 -3.09
susprindum_pbc                -2.26402   0.10393  1.27244   0.75037 -3.02
susprindum_mar                -1.64439   0.19313  1.51917   0.81467 -2.02
                              Pr(>|z|)    
tr_outcome.lag                 0.95195    
less_90d_tr1.lag               0.25073    
log_dias_treat_imp_sin_na.lag  0.04931 *  
comp_bpsc_y3_severe.lag        0.16369    
policonsumo2.lag               0.00952 ** 
edad_al_ing_1                  2.3e-07 ***
ano_nac_corr                   2.9e-07 ***
susinidum_oh                   0.00175 ** 
susinidum_coc                  0.03528 *  
susinidum_pbc                  0.04498 *  
susinidum_mar                  0.00029 ***
psycom_dum_with                0.70672    
psycom_dum_study               0.22807    
freq_cons_dum_5day             0.00302 ** 
cond_oc_dum_2inact             0.54687    
cond_oc_dum_3unemp             0.16003    
susprindum_oh                  4.4e-07 ***
susprindum_coc                 0.00197 ** 
susprindum_pbc                 0.00255 ** 
susprindum_mar                 0.04354 *  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                  1.06396      0.940  1.42e-01     7.993
less_90d_tr1.lag                3.04439      0.328  4.55e-01    20.350
log_dias_treat_imp_sin_na.lag   9.18970      0.109  1.01e+00    83.890
comp_bpsc_y3_severe.lag         0.06020     16.611  1.15e-03     3.140
policonsumo2.lag                0.00338    295.691  4.59e-05     0.249
edad_al_ing_1                   2.74615      0.364  1.87e+00     4.028
ano_nac_corr                    2.71284      0.369  1.85e+00     3.972
susinidum_oh                    0.34542      2.895  1.78e-01     0.672
susinidum_coc                   0.33172      3.015  1.19e-01     0.927
susinidum_pbc                   0.37837      2.643  1.46e-01     0.978
susinidum_mar                   0.30392      3.290  1.60e-01     0.579
psycom_dum_with                 1.10814      0.902  6.49e-01     1.892
psycom_dum_study                1.47935      0.676  7.83e-01     2.796
freq_cons_dum_5day              0.46583      2.147  2.81e-01     0.772
cond_oc_dum_2inact              0.71214      1.404  2.36e-01     2.149
cond_oc_dum_3unemp              0.47846      2.090  1.71e-01     1.338
susprindum_oh                   0.02890     34.598  7.30e-03     0.114
susprindum_coc                  0.08530     11.724  1.79e-02     0.406
susprindum_pbc                  0.10393      9.622  2.39e-02     0.452
susprindum_mar                  0.19313      5.178  3.91e-02     0.953

Concordance= 0.729  (se = 0.029 )
Likelihood ratio test= 53.7  on 20 df,   p=6e-05
Wald test            = 118  on 20 df,   p=8e-16
Score (logrank) test = 49  on 20 df,   p=3e-04,   Robust = 43.5  p=0.002

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
     17      68     123     305     261    3240 
[1] "Modality: WO intensive ambulatory\nTime interval: (93,135]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 41, number of events= 41 
   (36 observations deleted due to missingness)

                                 coef exp(coef) se(coef) robust se     z
tr_outcome.lag                 1.1304    3.0969   1.5680    0.9116  1.24
less_90d_tr1.lag                   NA        NA   0.0000    0.0000    NA
log_dias_treat_imp_sin_na.lag  0.1992    1.2204   0.2918    0.2185  0.91
comp_bpsc_y3_severe.lag       -3.0324    0.0482   1.7463    1.0916 -2.78
policonsumo2.lag              -3.5939    0.0275   2.0412    1.5108 -2.38
edad_al_ing_1                  0.4265    1.5319   0.3017    0.3236  1.32
ano_nac_corr                   0.3897    1.4765   0.2918    0.3157  1.23
susinidum_oh                  -1.2588    0.2840   0.6121    0.7170 -1.76
susinidum_coc                 -3.3142    0.0364   1.3930    1.0273 -3.23
susinidum_pbc                 -1.3977    0.2472   0.7332    0.8635 -1.62
susinidum_mar                      NA        NA   0.0000    0.0000    NA
psycom_dum_with                0.4333    1.5423   0.5645    0.5095  0.85
psycom_dum_study              -0.3400    0.7118   0.6199    0.6590 -0.52
freq_cons_dum_5day             1.9088    6.7448   0.6268    0.6090  3.13
cond_oc_dum_2inact            -1.7361    0.1762   0.9768    1.0968 -1.58
cond_oc_dum_3unemp            -1.7825    0.1682   1.0476    1.0569 -1.69
susprindum_oh                 -1.8979    0.1499   1.5268    1.5030 -1.26
susprindum_coc                -0.6726    0.5104   1.2308    0.7463 -0.90
susprindum_pbc                -0.6693    0.5121   1.1493    0.7270 -0.92
susprindum_mar                -1.7836    0.1680   1.4825    1.1096 -1.61
                              Pr(>|z|)   
tr_outcome.lag                  0.2149   
less_90d_tr1.lag                    NA   
log_dias_treat_imp_sin_na.lag   0.3620   
comp_bpsc_y3_severe.lag         0.0055 **
policonsumo2.lag                0.0174 * 
edad_al_ing_1                   0.1875   
ano_nac_corr                    0.2171   
susinidum_oh                    0.0791 . 
susinidum_coc                   0.0013 **
susinidum_pbc                   0.1055   
susinidum_mar                       NA   
psycom_dum_with                 0.3951   
psycom_dum_study                0.6059   
freq_cons_dum_5day              0.0017 **
cond_oc_dum_2inact              0.1135   
cond_oc_dum_3unemp              0.0917 . 
susprindum_oh                   0.2067   
susprindum_coc                  0.3675   
susprindum_pbc                  0.3572   
susprindum_mar                  0.1079   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                   3.0969      0.323   0.51881    18.486
less_90d_tr1.lag                     NA         NA        NA        NA
log_dias_treat_imp_sin_na.lag    1.2204      0.819   0.79528     1.873
comp_bpsc_y3_severe.lag          0.0482     20.748   0.00567     0.409
policonsumo2.lag                 0.0275     36.376   0.00142     0.531
edad_al_ing_1                    1.5319      0.653   0.81240     2.888
ano_nac_corr                     1.4765      0.677   0.79523     2.741
susinidum_oh                     0.2840      3.521   0.06966     1.158
susinidum_coc                    0.0364     27.502   0.00485     0.272
susinidum_pbc                    0.2472      4.046   0.04550     1.343
susinidum_mar                        NA         NA        NA        NA
psycom_dum_with                  1.5423      0.648   0.56817     4.187
psycom_dum_study                 0.7118      1.405   0.19560     2.590
freq_cons_dum_5day               6.7448      0.148   2.04461    22.250
cond_oc_dum_2inact               0.1762      5.675   0.02053     1.512
cond_oc_dum_3unemp               0.1682      5.945   0.02119     1.335
susprindum_oh                    0.1499      6.672   0.00788     2.852
susprindum_coc                   0.5104      1.959   0.11820     2.204
susprindum_pbc                   0.5121      1.953   0.12317     2.129
susprindum_mar                   0.1680      5.951   0.01910     1.479

Concordance= 0.714  (se = 0.049 )
Likelihood ratio test= 23.5  on 18 df,   p=0.2
Wald test            = 191  on 18 df,   p=<2e-16
Score (logrank) test = 26.4  on 18 df,   p=0.09,   Robust = 19.6  p=0.4

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
      1       7      15      34      32     354 
[1] "Modality: WO residential\nTime interval: (88,93]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 13, number of events= 13 
   (13 observations deleted due to missingness)

                                    coef  exp(coef)   se(coef)  robust se     z
tr_outcome.lag                        NA         NA   0.00e+00   0.00e+00    NA
less_90d_tr1.lag                      NA         NA   0.00e+00   0.00e+00    NA
log_dias_treat_imp_sin_na.lag         NA         NA   0.00e+00   0.00e+00    NA
comp_bpsc_y3_severe.lag               NA         NA   0.00e+00   0.00e+00    NA
policonsumo2.lag                      NA         NA   0.00e+00   0.00e+00    NA
edad_al_ing_1                  -5.69e+01   2.01e-25   5.83e+02   2.31e+00 -24.7
ano_nac_corr                   -3.70e+01   8.29e-17   2.42e+04   1.94e+00 -19.1
susinidum_oh                    2.40e+02  1.77e+104   3.87e+05   7.01e+00  34.2
susinidum_coc                         NA         NA   0.00e+00   0.00e+00    NA
susinidum_pbc                   1.68e+02   9.89e+72   1.69e+05   5.26e+00  31.9
susinidum_mar                         NA         NA   0.00e+00   0.00e+00    NA
psycom_dum_with                -2.17e+01   3.76e-10   7.26e+04   1.38e+00 -15.7
psycom_dum_study               -1.21e+02   1.81e-53   4.11e+05   5.48e+00 -22.2
freq_cons_dum_5day             -6.09e+01   3.71e-27   2.42e+04   2.26e+00 -26.9
cond_oc_dum_2inact                    NA         NA   0.00e+00   0.00e+00    NA
cond_oc_dum_3unemp                    NA         NA   0.00e+00   0.00e+00    NA
susprindum_oh                   9.63e+01   6.38e+41   1.21e+05   5.27e+00  18.2
susprindum_coc                  1.21e+02   2.66e+52   7.26e+04   3.47e+00  34.8
susprindum_pbc                  1.14e+02   3.44e+49   2.43e+04   4.27e+00  26.7
susprindum_mar                        NA         NA   0.00e+00   0.00e+00    NA
                              Pr(>|z|)    
tr_outcome.lag                      NA    
less_90d_tr1.lag                    NA    
log_dias_treat_imp_sin_na.lag       NA    
comp_bpsc_y3_severe.lag             NA    
policonsumo2.lag                    NA    
edad_al_ing_1                   <2e-16 ***
ano_nac_corr                    <2e-16 ***
susinidum_oh                    <2e-16 ***
susinidum_coc                       NA    
susinidum_pbc                   <2e-16 ***
susinidum_mar                       NA    
psycom_dum_with                 <2e-16 ***
psycom_dum_study                <2e-16 ***
freq_cons_dum_5day              <2e-16 ***
cond_oc_dum_2inact                  NA    
cond_oc_dum_3unemp                  NA    
susprindum_oh                   <2e-16 ***
susprindum_coc                  <2e-16 ***
susprindum_pbc                  <2e-16 ***
susprindum_mar                      NA    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                       NA         NA        NA        NA
less_90d_tr1.lag                     NA         NA        NA        NA
log_dias_treat_imp_sin_na.lag        NA         NA        NA        NA
comp_bpsc_y3_severe.lag              NA         NA        NA        NA
policonsumo2.lag                     NA         NA        NA        NA
edad_al_ing_1                  2.01e-25   4.98e+24  2.19e-27  1.84e-23
ano_nac_corr                   8.29e-17   1.21e+16  1.84e-18  3.73e-15
susinidum_oh                  1.77e+104  5.64e-105  1.92e+98 1.64e+110
susinidum_coc                        NA         NA        NA        NA
susinidum_pbc                  9.89e+72   1.01e-73  3.30e+68  2.97e+77
susinidum_mar                        NA         NA        NA        NA
psycom_dum_with                3.76e-10   2.66e+09  2.52e-11  5.61e-09
psycom_dum_study               1.81e-53   5.53e+52  3.91e-58  8.35e-49
freq_cons_dum_5day             3.71e-27   2.70e+26  4.43e-29  3.11e-25
cond_oc_dum_2inact                   NA         NA        NA        NA
cond_oc_dum_3unemp                   NA         NA        NA        NA
susprindum_oh                  6.38e+41   1.57e-42  2.07e+37  1.97e+46
susprindum_coc                 2.66e+52   3.76e-53  2.98e+49  2.37e+55
susprindum_pbc                 3.44e+49   2.90e-50  7.99e+45  1.48e+53
susprindum_mar                       NA         NA        NA        NA

Concordance= 1  (se = 0 )
Likelihood ratio test= 45.1  on 10 df,   p=2e-06
Wald test            = 1819  on 10 df,   p=<2e-16
Score (logrank) test = 16.9  on 10 df,   p=0.08,   Robust = 12.6  p=0.2

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
0.00e+00 0.00e+00 0.00e+00 2.29e+68 0.00e+00 2.98e+69 
[1] "Modality: GP intensive ambulatory\nTime interval: (88,93]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 74, number of events= 74 
   (74 observations deleted due to missingness)

                                 coef exp(coef) se(coef) robust se     z
tr_outcome.lag                     NA        NA   0.0000    0.0000    NA
less_90d_tr1.lag                   NA        NA   0.0000    0.0000    NA
log_dias_treat_imp_sin_na.lag      NA        NA   0.0000    0.0000    NA
comp_bpsc_y3_severe.lag            NA        NA   0.0000    0.0000    NA
policonsumo2.lag                   NA        NA   0.0000    0.0000    NA
edad_al_ing_1                  0.0225    1.0228   0.1475    0.1616  0.14
ano_nac_corr                   0.0460    1.0471   0.1395    0.1496  0.31
susinidum_oh                  -0.2291    0.7953   0.2985    0.2714 -0.84
susinidum_coc                  0.8542    2.3495   0.5729    0.5836  1.46
susinidum_pbc                 -0.6890    0.5021   0.5586    0.6669 -1.03
susinidum_mar                      NA        NA   0.0000    0.0000    NA
psycom_dum_with                0.3664    1.4425   0.3078    0.2991  1.22
psycom_dum_study               1.1222    3.0717   0.4851    0.4892  2.29
freq_cons_dum_5day             0.2846    1.3292   0.3179    0.2895  0.98
cond_oc_dum_2inact             0.1720    1.1877   0.4223    0.3660  0.47
cond_oc_dum_3unemp             0.4730    1.6048   0.3933    0.3670  1.29
susprindum_oh                 -0.0812    0.9220   0.6170    0.4583 -0.18
susprindum_coc                -0.0655    0.9366   0.6616    0.5184 -0.13
susprindum_pbc                -0.6944    0.4994   0.5289    0.3637 -1.91
susprindum_mar                     NA        NA   0.0000    0.0000    NA
                              Pr(>|z|)  
tr_outcome.lag                      NA  
less_90d_tr1.lag                    NA  
log_dias_treat_imp_sin_na.lag       NA  
comp_bpsc_y3_severe.lag             NA  
policonsumo2.lag                    NA  
edad_al_ing_1                    0.889  
ano_nac_corr                     0.758  
susinidum_oh                     0.399  
susinidum_coc                    0.143  
susinidum_pbc                    0.302  
susinidum_mar                       NA  
psycom_dum_with                  0.221  
psycom_dum_study                 0.022 *
freq_cons_dum_5day               0.326  
cond_oc_dum_2inact               0.638  
cond_oc_dum_3unemp               0.197  
susprindum_oh                    0.859  
susprindum_coc                   0.900  
susprindum_pbc                   0.056 .
susprindum_mar                      NA  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                       NA         NA        NA        NA
less_90d_tr1.lag                     NA         NA        NA        NA
log_dias_treat_imp_sin_na.lag        NA         NA        NA        NA
comp_bpsc_y3_severe.lag              NA         NA        NA        NA
policonsumo2.lag                     NA         NA        NA        NA
edad_al_ing_1                     1.023      0.978     0.745      1.40
ano_nac_corr                      1.047      0.955     0.781      1.40
susinidum_oh                      0.795      1.257     0.467      1.35
susinidum_coc                     2.350      0.426     0.749      7.37
susinidum_pbc                     0.502      1.992     0.136      1.86
susinidum_mar                        NA         NA        NA        NA
psycom_dum_with                   1.443      0.693     0.803      2.59
psycom_dum_study                  3.072      0.326     1.177      8.01
freq_cons_dum_5day                1.329      0.752     0.754      2.34
cond_oc_dum_2inact                1.188      0.842     0.580      2.43
cond_oc_dum_3unemp                1.605      0.623     0.782      3.29
susprindum_oh                     0.922      1.085     0.375      2.26
susprindum_coc                    0.937      1.068     0.339      2.59
susprindum_pbc                    0.499      2.002     0.245      1.02
susprindum_mar                       NA         NA        NA        NA

Concordance= 0.634  (se = 0.034 )
Likelihood ratio test= 13.9  on 13 df,   p=0.4
Wald test            = 23.4  on 13 df,   p=0.04
Score (logrank) test = 14.5  on 13 df,   p=0.3,   Robust = 16.6  p=0.2

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.198   0.615   0.804   0.936   1.277   2.215 
[1] "Modality: basic ambulatory\nTime interval: (88,93]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 77, number of events= 77 
   (76 observations deleted due to missingness)

                                   coef exp(coef)  se(coef) robust se      z
tr_outcome.lag                       NA        NA  0.00e+00  0.00e+00     NA
less_90d_tr1.lag                     NA        NA  0.00e+00  0.00e+00     NA
log_dias_treat_imp_sin_na.lag -4.77e-01  6.21e-01  4.43e-01  2.14e-01  -2.23
comp_bpsc_y3_severe.lag              NA        NA  0.00e+00  0.00e+00     NA
policonsumo2.lag                     NA        NA  0.00e+00  0.00e+00     NA
edad_al_ing_1                 -8.12e-02  9.22e-01  1.57e-01  1.39e-01  -0.58
ano_nac_corr                  -1.72e-01  8.42e-01  1.59e-01  1.40e-01  -1.23
susinidum_oh                  -6.05e-01  5.46e-01  6.92e-01  4.73e-01  -1.28
susinidum_coc                 -8.26e-02  9.21e-01  1.05e+00  6.08e-01  -0.14
susinidum_pbc                 -1.81e+00  1.64e-01  8.99e-01  6.97e-01  -2.59
susinidum_mar                 -8.53e-01  4.26e-01  7.36e-01  5.27e-01  -1.62
psycom_dum_with               -3.47e-01  7.07e-01  3.23e-01  2.73e-01  -1.27
psycom_dum_study              -6.93e-01  5.00e-01  3.65e-01  3.41e-01  -2.03
freq_cons_dum_5day            -2.08e-01  8.12e-01  3.02e-01  2.87e-01  -0.73
cond_oc_dum_2inact             7.39e-01  2.09e+00  4.50e-01  4.40e-01   1.68
cond_oc_dum_3unemp             4.68e-01  1.60e+00  3.39e-01  3.44e-01   1.36
susprindum_oh                 -2.10e+01  7.35e-10  3.08e+03  6.52e-01 -32.26
susprindum_coc                -2.08e+01  9.01e-10  3.08e+03  5.09e-01 -40.94
susprindum_pbc                -1.95e+01  3.43e-09  3.08e+03  5.67e-01 -34.39
susprindum_mar                -2.06e+01  1.17e-09  3.08e+03  6.89e-01 -29.86
                              Pr(>|z|)    
tr_outcome.lag                      NA    
less_90d_tr1.lag                    NA    
log_dias_treat_imp_sin_na.lag   0.0258 *  
comp_bpsc_y3_severe.lag             NA    
policonsumo2.lag                    NA    
edad_al_ing_1                   0.5597    
ano_nac_corr                    0.2186    
susinidum_oh                    0.2008    
susinidum_coc                   0.8919    
susinidum_pbc                   0.0095 ** 
susinidum_mar                   0.1056    
psycom_dum_with                 0.2028    
psycom_dum_study                0.0423 *  
freq_cons_dum_5day              0.4680    
cond_oc_dum_2inact              0.0926 .  
cond_oc_dum_3unemp              0.1732    
susprindum_oh                   <2e-16 ***
susprindum_coc                  <2e-16 ***
susprindum_pbc                  <2e-16 ***
susprindum_mar                  <2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                       NA         NA        NA        NA
less_90d_tr1.lag                     NA         NA        NA        NA
log_dias_treat_imp_sin_na.lag  6.21e-01   1.61e+00  4.08e-01  9.44e-01
comp_bpsc_y3_severe.lag              NA         NA        NA        NA
policonsumo2.lag                     NA         NA        NA        NA
edad_al_ing_1                  9.22e-01   1.08e+00  7.02e-01  1.21e+00
ano_nac_corr                   8.42e-01   1.19e+00  6.41e-01  1.11e+00
susinidum_oh                   5.46e-01   1.83e+00  2.16e-01  1.38e+00
susinidum_coc                  9.21e-01   1.09e+00  2.80e-01  3.03e+00
susinidum_pbc                  1.64e-01   6.11e+00  4.17e-02  6.43e-01
susinidum_mar                  4.26e-01   2.35e+00  1.52e-01  1.20e+00
psycom_dum_with                7.07e-01   1.41e+00  4.14e-01  1.21e+00
psycom_dum_study               5.00e-01   2.00e+00  2.56e-01  9.76e-01
freq_cons_dum_5day             8.12e-01   1.23e+00  4.63e-01  1.42e+00
cond_oc_dum_2inact             2.09e+00   4.77e-01  8.85e-01  4.96e+00
cond_oc_dum_3unemp             1.60e+00   6.26e-01  8.14e-01  3.13e+00
susprindum_oh                  7.35e-10   1.36e+09  2.05e-10  2.64e-09
susprindum_coc                 9.01e-10   1.11e+09  3.33e-10  2.44e-09
susprindum_pbc                 3.43e-09   2.91e+08  1.13e-09  1.04e-08
susprindum_mar                 1.17e-09   8.58e+08  3.02e-10  4.50e-09

Concordance= 0.731  (se = 0.03 )
Likelihood ratio test= 44.5  on 16 df,   p=2e-04
Wald test            = 16181  on 16 df,   p=<2e-16
Score (logrank) test = 111  on 16 df,   p=2e-16,   Robust = 37.4  p=0.002

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
1.00e+00 7.19e+08 1.20e+09 1.84e+09 2.58e+09 7.16e+09 
[1] "Modality: GP residential\nTime interval: (88,93]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 44, number of events= 44 
   (44 observations deleted due to missingness)

                                 coef exp(coef) se(coef) robust se     z
tr_outcome.lag                     NA        NA   0.0000    0.0000    NA
less_90d_tr1.lag                   NA        NA   0.0000    0.0000    NA
log_dias_treat_imp_sin_na.lag      NA        NA   0.0000    0.0000    NA
comp_bpsc_y3_severe.lag            NA        NA   0.0000    0.0000    NA
policonsumo2.lag                   NA        NA   0.0000    0.0000    NA
edad_al_ing_1                 -0.0941    0.9102   0.2559    0.2579 -0.36
ano_nac_corr                  -0.1054    0.9000   0.2603    0.2672 -0.39
susinidum_oh                  -1.3805    0.2515   1.1947    0.7912 -1.74
susinidum_coc                 -2.3484    0.0955   1.7688    1.0683 -2.20
susinidum_pbc                 -1.7372    0.1760   1.2633    0.9136 -1.90
susinidum_mar                 -1.7400    0.1755   1.2540    0.9334 -1.86
psycom_dum_with               -0.5855    0.5568   0.4569    0.4924 -1.19
psycom_dum_study              -0.5570    0.5729   0.7513    0.6086 -0.92
freq_cons_dum_5day             0.2100    1.2337   0.4609    0.4900  0.43
cond_oc_dum_2inact             1.6085    4.9952   0.8868    0.6243  2.58
cond_oc_dum_3unemp             0.6958    2.0053   0.6945    0.7280  0.96
susprindum_oh                 -3.2052    0.0406   1.4883    1.2502 -2.56
susprindum_coc                -2.1759    0.1135   1.4852    1.1353 -1.92
susprindum_pbc                -2.7616    0.0632   1.2925    0.9600 -2.88
susprindum_mar                     NA        NA   0.0000    0.0000    NA
                              Pr(>|z|)   
tr_outcome.lag                      NA   
less_90d_tr1.lag                    NA   
log_dias_treat_imp_sin_na.lag       NA   
comp_bpsc_y3_severe.lag             NA   
policonsumo2.lag                    NA   
edad_al_ing_1                    0.715   
ano_nac_corr                     0.693   
susinidum_oh                     0.081 . 
susinidum_coc                    0.028 * 
susinidum_pbc                    0.057 . 
susinidum_mar                    0.062 . 
psycom_dum_with                  0.234   
psycom_dum_study                 0.360   
freq_cons_dum_5day               0.668   
cond_oc_dum_2inact               0.010 **
cond_oc_dum_3unemp               0.339   
susprindum_oh                    0.010 * 
susprindum_coc                   0.055 . 
susprindum_pbc                   0.004 **
susprindum_mar                      NA   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                       NA         NA        NA        NA
less_90d_tr1.lag                     NA         NA        NA        NA
log_dias_treat_imp_sin_na.lag        NA         NA        NA        NA
comp_bpsc_y3_severe.lag              NA         NA        NA        NA
policonsumo2.lag                     NA         NA        NA        NA
edad_al_ing_1                    0.9102      1.099   0.54904     1.509
ano_nac_corr                     0.9000      1.111   0.53305     1.520
susinidum_oh                     0.2515      3.977   0.05333     1.186
susinidum_coc                    0.0955     10.469   0.01177     0.775
susinidum_pbc                    0.1760      5.682   0.02937     1.055
susinidum_mar                    0.1755      5.697   0.02817     1.094
psycom_dum_with                  0.5568      1.796   0.21212     1.462
psycom_dum_study                 0.5729      1.745   0.17380     1.889
freq_cons_dum_5day               1.2337      0.811   0.47219     3.223
cond_oc_dum_2inact               4.9952      0.200   1.46948    16.980
cond_oc_dum_3unemp               2.0053      0.499   0.48140     8.353
susprindum_oh                    0.0406     24.660   0.00350     0.470
susprindum_coc                   0.1135      8.810   0.01227     1.050
susprindum_pbc                   0.0632     15.825   0.00963     0.415
susprindum_mar                       NA         NA        NA        NA

Concordance= 0.62  (se = 0.051 )
Likelihood ratio test= 12.3  on 14 df,   p=0.6
Wald test            = 38.5  on 14 df,   p=4e-04
Score (logrank) test = 20  on 14 df,   p=0.1,   Robust = 11.2  p=0.7

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
    2.6    34.2    48.9    52.1    70.7    99.9 
[1] "Modality: WO intensive ambulatory\nTime interval: (88,93]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 10, number of events= 10 
   (10 observations deleted due to missingness)

                                   coef exp(coef)  se(coef) robust se      z
tr_outcome.lag                       NA        NA  0.00e+00  0.00e+00     NA
less_90d_tr1.lag                     NA        NA  0.00e+00  0.00e+00     NA
log_dias_treat_imp_sin_na.lag        NA        NA  0.00e+00  0.00e+00     NA
comp_bpsc_y3_severe.lag              NA        NA  0.00e+00  0.00e+00     NA
policonsumo2.lag                     NA        NA  0.00e+00  0.00e+00     NA
edad_al_ing_1                 -5.34e+01  6.69e-24  6.64e+03  1.45e+00 -36.92
ano_nac_corr                  -4.99e+01  2.05e-22  5.79e+03  1.40e+00 -35.58
susinidum_oh                   5.70e+00  2.99e+02  1.73e+03  1.07e+00   5.31
susinidum_coc                        NA        NA  0.00e+00  0.00e+00     NA
susinidum_pbc                  5.46e+01  5.41e+23  1.65e+04  3.47e+00  15.75
susinidum_mar                        NA        NA  0.00e+00  0.00e+00     NA
psycom_dum_with                2.74e+01  8.14e+11  4.34e+03  1.45e+00  18.95
psycom_dum_study                     NA        NA  0.00e+00  0.00e+00     NA
freq_cons_dum_5day            -1.48e+02  6.27e-65  1.42e+04  3.58e+00 -41.25
cond_oc_dum_2inact            -4.10e+01  1.49e-18  8.48e+03  2.02e+00 -20.30
cond_oc_dum_3unemp                   NA        NA  0.00e+00  0.00e+00     NA
susprindum_oh                  7.39e+01  1.23e+32  6.98e+03  3.27e+00  22.57
susprindum_coc                 1.39e+02  1.89e+60  6.56e+03  3.67e+00  37.84
susprindum_pbc                       NA        NA  0.00e+00  0.00e+00     NA
susprindum_mar                       NA        NA  0.00e+00  0.00e+00     NA
                              Pr(>|z|)    
tr_outcome.lag                      NA    
less_90d_tr1.lag                    NA    
log_dias_treat_imp_sin_na.lag       NA    
comp_bpsc_y3_severe.lag             NA    
policonsumo2.lag                    NA    
edad_al_ing_1                  < 2e-16 ***
ano_nac_corr                   < 2e-16 ***
susinidum_oh                   1.1e-07 ***
susinidum_coc                       NA    
susinidum_pbc                  < 2e-16 ***
susinidum_mar                       NA    
psycom_dum_with                < 2e-16 ***
psycom_dum_study                    NA    
freq_cons_dum_5day             < 2e-16 ***
cond_oc_dum_2inact             < 2e-16 ***
cond_oc_dum_3unemp                  NA    
susprindum_oh                  < 2e-16 ***
susprindum_coc                 < 2e-16 ***
susprindum_pbc                      NA    
susprindum_mar                      NA    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                       NA         NA        NA        NA
less_90d_tr1.lag                     NA         NA        NA        NA
log_dias_treat_imp_sin_na.lag        NA         NA        NA        NA
comp_bpsc_y3_severe.lag              NA         NA        NA        NA
policonsumo2.lag                     NA         NA        NA        NA
edad_al_ing_1                  6.69e-24   1.50e+23  3.94e-25  1.14e-22
ano_nac_corr                   2.05e-22   4.87e+21  1.31e-23  3.21e-21
susinidum_oh                   2.99e+02   3.34e-03  3.64e+01  2.45e+03
susinidum_coc                        NA         NA        NA        NA
susinidum_pbc                  5.41e+23   1.85e-24  6.03e+20  4.86e+26
susinidum_mar                        NA         NA        NA        NA
psycom_dum_with                8.14e+11   1.23e-12  4.77e+10  1.39e+13
psycom_dum_study                     NA         NA        NA        NA
freq_cons_dum_5day             6.27e-65   1.59e+64  5.58e-68  7.05e-62
cond_oc_dum_2inact             1.49e-18   6.70e+17  2.84e-20  7.85e-17
cond_oc_dum_3unemp                   NA         NA        NA        NA
susprindum_oh                  1.23e+32   8.13e-33  2.01e+29  7.53e+34
susprindum_coc                 1.89e+60   5.28e-61  1.43e+57  2.51e+63
susprindum_pbc                       NA         NA        NA        NA
susprindum_mar                       NA         NA        NA        NA

Concordance= 1  (se = 0 )
Likelihood ratio test= 30.2  on 9 df,   p=4e-04
Wald test            = 1857  on 9 df,   p=<2e-16
Score (logrank) test = 21.2  on 9 df,   p=0.01,   Robust = 9.99  p=0.4

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
0.00e+00 0.00e+00 8.00e+01 1.71e+24 2.95e+13 1.71e+25 
Code
invisible("This model had some problems, not stable (low n in strata)")

invisible("Did it later, at 2024-05-16")

iiws_strat1 <- list()

comb_strata1 <- expand.grid(unique(data_mine_miss_restr_proc2$tipo_de_plan_2_mod), 
                           unique(data_mine_miss_restr_proc2$time_interval3))

for (xx in seq_along(1:nrow(comb_strata1))) {
  i<- comb_strata1[xx,"Var1"]
  j<- comb_strata1[xx,"Var2"]
  data_mine_miss_proc2_subset<-
  subset(data_mine_miss_restr_proc2, subset= tipo_de_plan_2_mod==i & time_interval3==j)
  maxfu_df_subset <- subset(maxfu_restr_df, subset= maxfu.id %in% unique(data_mine_miss_proc2_subset$id))
  iiw_model_strat1<-
    IrregLong::iiw.weights(Surv(time.lag,time,event)~ 
          cluster(id)+ #If a frailty model is used, the cluster(id) term should appear before other covariates
          tr_outcome.lag+
          less_90d_tr1.lag+
          log_dias_treat_imp_sin_na.lag +
          comp_bpsc_y3_severe.lag+
          policonsumo2.lag + 
          edad_al_ing_1 + 
          ano_nac_corr + 
          susinidum_oh +
          susinidum_coc +
          susinidum_pbc +
          susinidum_mar +
          psycom_dum_with +
          psycom_dum_study + 
          freq_cons_dum_5day +
          cond_oc_dum_2inact +
          cond_oc_dum_3unemp +
          susprindum_oh +
          susprindum_coc +
          susprindum_pbc +
          susprindum_mar, 
      data= data_mine_miss_proc2_subset,
      id= "id",
      time= "time",
      event= "event", #character string indicating which column of the data indicates whether or not a visit occurred. If every row corresponds to a visit, then this column will consist entirely of ones
      maxfu= maxfu_df_subset,
      invariant= c("edad_al_ing_1", "ano_nac_corr", "susinidum_oh", "susinidum_coc", "susinidum_pbc", "susinidum_mar",  "psycom_dum_with", "psycom_dum_study_rec2", "freq_cons_dum_5day", "cond_oc_dum_3unemp", "cond_oc_dum_2inact", "susprindum_oh", "susprindum_coc", "susprindum_pbc", "susprindum_mar"),
      lagvars= c("time", "tr_outcome","log_dias_treat_imp_sin_na", "less_90d_tr1","comp_bpsc_y3_severe", "policonsumo2"),
      lagfirst= c(2.95082,1,4.499811,1,1,1),  #90/30.5 4.499811 es 90 días
      first= T
)
  
  iiws_strat1[[xx]] <- list(
      model = iiw_model_strat1,
      comb = paste0("Modality: ",i,"\nTime interval: ", j,"."),
      cox_iiw = summary(iiw_model_strat1$m),
      iiw = summary(iiw_model_strat1$iiw)
  )
  print(paste0("Modality: ",i,"\nTime interval: ", j))
  print(summary(iiw_model_strat1$m))
  print(summary(iiw_model_strat1$iiw))

}
[1] "Modality: WO residential\nTime interval: [0,88]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 2106, number of events= 2106 
   (1417 observations deleted due to missingness)

                                  coef exp(coef) se(coef) robust se     z
tr_outcome.lag                 0.20536   1.22796  0.06616   0.05418  3.79
less_90d_tr1.lag               0.80271   2.23158  0.07067   0.07676 10.46
log_dias_treat_imp_sin_na.lag  0.17616   1.19262  0.03140   0.03679  4.79
comp_bpsc_y3_severe.lag        0.17454   1.19070  0.06655   0.07093  2.46
policonsumo2.lag              -0.04576   0.95527  0.07395   0.05745 -0.80
edad_al_ing_1                  0.11274   1.11935  0.01083   0.00973 11.59
ano_nac_corr                   0.11840   1.12569  0.01057   0.00935 12.66
susinidum_oh                   0.03767   1.03839  0.16591   0.14173  0.27
susinidum_coc                  0.08672   1.09060  0.18995   0.16127  0.54
susinidum_pbc                  0.02071   1.02093  0.17193   0.15333  0.14
susinidum_mar                  0.08361   1.08721  0.16692   0.14457  0.58
psycom_dum_with               -0.02497   0.97534  0.06301   0.05548 -0.45
psycom_dum_study              -0.39981   0.67045  0.07750   0.07329 -5.45
freq_cons_dum_5day            -0.04344   0.95749  0.05145   0.05074 -0.86
cond_oc_dum_2inact             0.03819   1.03893  0.08141   0.08997  0.42
cond_oc_dum_3unemp             0.08288   1.08641  0.08212   0.09092  0.91
susprindum_oh                 -0.03090   0.96957  0.15198   0.14127 -0.22
susprindum_coc                -0.10130   0.90367  0.15487   0.14385 -0.70
susprindum_pbc                -0.06907   0.93326  0.14478   0.13566 -0.51
susprindum_mar                -0.25578   0.77432  0.21774   0.20267 -1.26
                              Pr(>|z|)    
tr_outcome.lag                 0.00015 ***
less_90d_tr1.lag               < 2e-16 ***
log_dias_treat_imp_sin_na.lag  1.7e-06 ***
comp_bpsc_y3_severe.lag        0.01386 *  
policonsumo2.lag               0.42565    
edad_al_ing_1                  < 2e-16 ***
ano_nac_corr                   < 2e-16 ***
susinidum_oh                   0.79041    
susinidum_coc                  0.59075    
susinidum_pbc                  0.89254    
susinidum_mar                  0.56304    
psycom_dum_with                0.65264    
psycom_dum_study               4.9e-08 ***
freq_cons_dum_5day             0.39189    
cond_oc_dum_2inact             0.67120    
cond_oc_dum_3unemp             0.36200    
susprindum_oh                  0.82687    
susprindum_coc                 0.48131    
susprindum_pbc                 0.61062    
susprindum_mar                 0.20693    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    1.228      0.814     1.104     1.366
less_90d_tr1.lag                  2.232      0.448     1.920     2.594
log_dias_treat_imp_sin_na.lag     1.193      0.838     1.110     1.282
comp_bpsc_y3_severe.lag           1.191      0.840     1.036     1.368
policonsumo2.lag                  0.955      1.047     0.854     1.069
edad_al_ing_1                     1.119      0.893     1.098     1.141
ano_nac_corr                      1.126      0.888     1.105     1.147
susinidum_oh                      1.038      0.963     0.787     1.371
susinidum_coc                     1.091      0.917     0.795     1.496
susinidum_pbc                     1.021      0.979     0.756     1.379
susinidum_mar                     1.087      0.920     0.819     1.443
psycom_dum_with                   0.975      1.025     0.875     1.087
psycom_dum_study                  0.670      1.492     0.581     0.774
freq_cons_dum_5day                0.957      1.044     0.867     1.058
cond_oc_dum_2inact                1.039      0.963     0.871     1.239
cond_oc_dum_3unemp                1.086      0.920     0.909     1.298
susprindum_oh                     0.970      1.031     0.735     1.279
susprindum_coc                    0.904      1.107     0.682     1.198
susprindum_pbc                    0.933      1.072     0.715     1.218
susprindum_mar                    0.774      1.291     0.520     1.152

Concordance= 0.626  (se = 0.005 )
Likelihood ratio test= 328  on 20 df,   p=<2e-16
Wald test            = 374  on 20 df,   p=<2e-16
Score (logrank) test = 315  on 20 df,   p=<2e-16,   Robust = 350  p=<2e-16

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.15    0.36    0.49    0.58    0.70   12.82 
[1] "Modality: GP intensive ambulatory\nTime interval: [0,88]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 9973, number of events= 9973 
   (6233 observations deleted due to missingness)

                                  coef exp(coef) se(coef) robust se      z
tr_outcome.lag                 0.20542   1.22804  0.03520   0.03199   6.42
less_90d_tr1.lag               1.03973   2.82845  0.03442   0.03813  27.26
log_dias_treat_imp_sin_na.lag  0.22867   1.25693  0.02066   0.02551   8.97
comp_bpsc_y3_severe.lag        0.36576   1.44161  0.02292   0.02274  16.09
policonsumo2.lag               0.16091   1.17458  0.03411   0.03307   4.87
edad_al_ing_1                  0.13636   1.14609  0.00500   0.00450  30.29
ano_nac_corr                   0.13706   1.14690  0.00492   0.00442  30.99
susinidum_oh                   0.09086   1.09511  0.06911   0.06255   1.45
susinidum_coc                  0.07104   1.07362  0.08273   0.08140   0.87
susinidum_pbc                  0.17770   1.19447  0.07839   0.07475   2.38
susinidum_mar                  0.17350   1.18946  0.07042   0.06400   2.71
psycom_dum_with               -0.03422   0.96636  0.02230   0.02177  -1.57
psycom_dum_study              -0.42746   0.65217  0.03271   0.03794 -11.27
freq_cons_dum_5day            -0.00842   0.99161  0.02065   0.02115  -0.40
cond_oc_dum_2inact             0.00653   1.00655  0.03106   0.02998   0.22
cond_oc_dum_3unemp             0.01222   1.01230  0.02224   0.02276   0.54
susprindum_oh                 -0.06775   0.93449  0.08948   0.09220  -0.73
susprindum_coc                -0.02664   0.97371  0.09056   0.09304  -0.29
susprindum_pbc                -0.01332   0.98677  0.08833   0.09113  -0.15
susprindum_mar                 0.01243   1.01251  0.09711   0.09863   0.13
                              Pr(>|z|)    
tr_outcome.lag                 1.3e-10 ***
less_90d_tr1.lag               < 2e-16 ***
log_dias_treat_imp_sin_na.lag  < 2e-16 ***
comp_bpsc_y3_severe.lag        < 2e-16 ***
policonsumo2.lag               1.1e-06 ***
edad_al_ing_1                  < 2e-16 ***
ano_nac_corr                   < 2e-16 ***
susinidum_oh                    0.1464    
susinidum_coc                   0.3828    
susinidum_pbc                   0.0174 *  
susinidum_mar                   0.0067 ** 
psycom_dum_with                 0.1160    
psycom_dum_study               < 2e-16 ***
freq_cons_dum_5day              0.6904    
cond_oc_dum_2inact              0.8276    
cond_oc_dum_3unemp              0.5913    
susprindum_oh                   0.4624    
susprindum_coc                  0.7746    
susprindum_pbc                  0.8838    
susprindum_mar                  0.8997    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    1.228      0.814     1.153     1.308
less_90d_tr1.lag                  2.828      0.354     2.625     3.048
log_dias_treat_imp_sin_na.lag     1.257      0.796     1.196     1.321
comp_bpsc_y3_severe.lag           1.442      0.694     1.379     1.507
policonsumo2.lag                  1.175      0.851     1.101     1.253
edad_al_ing_1                     1.146      0.873     1.136     1.156
ano_nac_corr                      1.147      0.872     1.137     1.157
susinidum_oh                      1.095      0.913     0.969     1.238
susinidum_coc                     1.074      0.931     0.915     1.259
susinidum_pbc                     1.194      0.837     1.032     1.383
susinidum_mar                     1.189      0.841     1.049     1.348
psycom_dum_with                   0.966      1.035     0.926     1.008
psycom_dum_study                  0.652      1.533     0.605     0.703
freq_cons_dum_5day                0.992      1.008     0.951     1.034
cond_oc_dum_2inact                1.007      0.993     0.949     1.067
cond_oc_dum_3unemp                1.012      0.988     0.968     1.058
susprindum_oh                     0.934      1.070     0.780     1.120
susprindum_coc                    0.974      1.027     0.811     1.169
susprindum_pbc                    0.987      1.013     0.825     1.180
susprindum_mar                    1.013      0.988     0.835     1.228

Concordance= 0.661  (se = 0.002 )
Likelihood ratio test= 2547  on 20 df,   p=<2e-16
Wald test            = 2470  on 20 df,   p=<2e-16
Score (logrank) test = 2507  on 20 df,   p=<2e-16,   Robust = 2298  p=<2e-16

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.07    0.18    0.29    0.38    0.50   16.37 
[1] "Modality: basic ambulatory\nTime interval: [0,88]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 8836, number of events= 8836 
   (5238 observations deleted due to missingness)

                                  coef exp(coef) se(coef) robust se     z
tr_outcome.lag                 0.19144   1.21100  0.03883   0.03485  5.49
less_90d_tr1.lag               0.85480   2.35091  0.03764   0.04065 21.03
log_dias_treat_imp_sin_na.lag  0.15761   1.17071  0.02305   0.02568  6.14
comp_bpsc_y3_severe.lag        0.71607   2.04638  0.02649   0.02951 24.26
policonsumo2.lag               0.05970   1.06152  0.03302   0.02996  1.99
edad_al_ing_1                  0.12723   1.13568  0.00543   0.00497 25.62
ano_nac_corr                   0.12930   1.13803  0.00536   0.00489 26.45
susinidum_oh                   0.06745   1.06978  0.07583   0.06475  1.04
susinidum_coc                  0.10834   1.11443  0.08975   0.08886  1.22
susinidum_pbc                  0.15305   1.16538  0.08700   0.07998  1.91
susinidum_mar                  0.12643   1.13477  0.07701   0.06677  1.89
psycom_dum_with               -0.09434   0.90997  0.02377   0.02360 -4.00
psycom_dum_study              -0.22528   0.79829  0.03267   0.03752 -6.00
freq_cons_dum_5day            -0.05778   0.94386  0.02458   0.02544 -2.27
cond_oc_dum_2inact            -0.03710   0.96358  0.03369   0.03624 -1.02
cond_oc_dum_3unemp            -0.01370   0.98639  0.02594   0.02723 -0.50
susprindum_oh                  0.03720   1.03790  0.10027   0.07352  0.51
susprindum_coc                 0.04959   1.05084  0.10158   0.07538  0.66
susprindum_pbc                 0.04364   1.04461  0.10005   0.07375  0.59
susprindum_mar                 0.08895   1.09303  0.10631   0.08072  1.10
                              Pr(>|z|)    
tr_outcome.lag                 3.9e-08 ***
less_90d_tr1.lag               < 2e-16 ***
log_dias_treat_imp_sin_na.lag  8.3e-10 ***
comp_bpsc_y3_severe.lag        < 2e-16 ***
policonsumo2.lag                 0.046 *  
edad_al_ing_1                  < 2e-16 ***
ano_nac_corr                   < 2e-16 ***
susinidum_oh                     0.298    
susinidum_coc                    0.223    
susinidum_pbc                    0.056 .  
susinidum_mar                    0.058 .  
psycom_dum_with                6.4e-05 ***
psycom_dum_study               1.9e-09 ***
freq_cons_dum_5day               0.023 *  
cond_oc_dum_2inact               0.306    
cond_oc_dum_3unemp               0.615    
susprindum_oh                    0.613    
susprindum_coc                   0.511    
susprindum_pbc                   0.554    
susprindum_mar                   0.270    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    1.211      0.826     1.131     1.297
less_90d_tr1.lag                  2.351      0.425     2.171     2.546
log_dias_treat_imp_sin_na.lag     1.171      0.854     1.113     1.231
comp_bpsc_y3_severe.lag           2.046      0.489     1.931     2.168
policonsumo2.lag                  1.062      0.942     1.001     1.126
edad_al_ing_1                     1.136      0.881     1.125     1.147
ano_nac_corr                      1.138      0.879     1.127     1.149
susinidum_oh                      1.070      0.935     0.942     1.215
susinidum_coc                     1.114      0.897     0.936     1.326
susinidum_pbc                     1.165      0.858     0.996     1.363
susinidum_mar                     1.135      0.881     0.996     1.293
psycom_dum_with                   0.910      1.099     0.869     0.953
psycom_dum_study                  0.798      1.253     0.742     0.859
freq_cons_dum_5day                0.944      1.059     0.898     0.992
cond_oc_dum_2inact                0.964      1.038     0.898     1.035
cond_oc_dum_3unemp                0.986      1.014     0.935     1.040
susprindum_oh                     1.038      0.963     0.899     1.199
susprindum_coc                    1.051      0.952     0.907     1.218
susprindum_pbc                    1.045      0.957     0.904     1.207
susprindum_mar                    1.093      0.915     0.933     1.280

Concordance= 0.678  (se = 0.003 )
Likelihood ratio test= 2793  on 20 df,   p=<2e-16
Wald test            = 2423  on 20 df,   p=<2e-16
Score (logrank) test = 2875  on 20 df,   p=<2e-16,   Robust = 2414  p=<2e-16

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.06    0.17    0.29    0.42    0.62    9.17 
[1] "Modality: GP residential\nTime interval: [0,88]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 4436, number of events= 4436 
   (2815 observations deleted due to missingness)

                                  coef exp(coef) se(coef) robust se     z
tr_outcome.lag                 0.30950   1.36275  0.04465   0.03996  7.75
less_90d_tr1.lag               0.77198   2.16405  0.04672   0.05172 14.93
log_dias_treat_imp_sin_na.lag  0.19686   1.21757  0.02213   0.02760  7.13
comp_bpsc_y3_severe.lag        0.33683   1.40050  0.04277   0.03970  8.49
policonsumo2.lag               0.21234   1.23657  0.05514   0.05263  4.03
edad_al_ing_1                  0.10459   1.11026  0.00769   0.00679 15.41
ano_nac_corr                   0.10628   1.11214  0.00765   0.00667 15.94
susinidum_oh                   0.09816   1.10314  0.10611   0.10080  0.97
susinidum_coc                  0.16421   1.17846  0.12980   0.12460  1.32
susinidum_pbc                  0.23338   1.26286  0.11301   0.11103  2.10
susinidum_mar                  0.24446   1.27694  0.10643   0.10096  2.42
psycom_dum_with               -0.04315   0.95777  0.03427   0.03465 -1.25
psycom_dum_study              -0.31271   0.73146  0.04962   0.05491 -5.69
freq_cons_dum_5day             0.01423   1.01433  0.03482   0.03425  0.42
cond_oc_dum_2inact             0.07737   1.08044  0.05583   0.05164  1.50
cond_oc_dum_3unemp             0.09295   1.09740  0.04599   0.04394  2.12
susprindum_oh                  0.08714   1.09105  0.16038   0.15169  0.57
susprindum_coc                -0.00799   0.99204  0.16159   0.15287 -0.05
susprindum_pbc                -0.06583   0.93629  0.15656   0.14868 -0.44
susprindum_mar                 0.10378   1.10935  0.18041   0.16729  0.62
                              Pr(>|z|)    
tr_outcome.lag                 9.5e-15 ***
less_90d_tr1.lag               < 2e-16 ***
log_dias_treat_imp_sin_na.lag  9.9e-13 ***
comp_bpsc_y3_severe.lag        < 2e-16 ***
policonsumo2.lag               5.5e-05 ***
edad_al_ing_1                  < 2e-16 ***
ano_nac_corr                   < 2e-16 ***
susinidum_oh                     0.330    
susinidum_coc                    0.188    
susinidum_pbc                    0.036 *  
susinidum_mar                    0.015 *  
psycom_dum_with                  0.213    
psycom_dum_study               1.2e-08 ***
freq_cons_dum_5day               0.678    
cond_oc_dum_2inact               0.134    
cond_oc_dum_3unemp               0.034 *  
susprindum_oh                    0.566    
susprindum_coc                   0.958    
susprindum_pbc                   0.658    
susprindum_mar                   0.535    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    1.363      0.734     1.260     1.474
less_90d_tr1.lag                  2.164      0.462     1.955     2.395
log_dias_treat_imp_sin_na.lag     1.218      0.821     1.153     1.285
comp_bpsc_y3_severe.lag           1.401      0.714     1.296     1.514
policonsumo2.lag                  1.237      0.809     1.115     1.371
edad_al_ing_1                     1.110      0.901     1.096     1.125
ano_nac_corr                      1.112      0.899     1.098     1.127
susinidum_oh                      1.103      0.907     0.905     1.344
susinidum_coc                     1.178      0.849     0.923     1.504
susinidum_pbc                     1.263      0.792     1.016     1.570
susinidum_mar                     1.277      0.783     1.048     1.556
psycom_dum_with                   0.958      1.044     0.895     1.025
psycom_dum_study                  0.731      1.367     0.657     0.815
freq_cons_dum_5day                1.014      0.986     0.948     1.085
cond_oc_dum_2inact                1.080      0.926     0.976     1.196
cond_oc_dum_3unemp                1.097      0.911     1.007     1.196
susprindum_oh                     1.091      0.917     0.810     1.469
susprindum_coc                    0.992      1.008     0.735     1.339
susprindum_pbc                    0.936      1.068     0.700     1.253
susprindum_mar                    1.109      0.901     0.799     1.540

Concordance= 0.642  (se = 0.004 )
Likelihood ratio test= 828  on 20 df,   p=<2e-16
Wald test            = 790  on 20 df,   p=<2e-16
Score (logrank) test = 780  on 20 df,   p=<2e-16,   Robust = 724  p=<2e-16

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.08    0.18    0.24    0.30    0.37    6.84 
[1] "Modality: WO intensive ambulatory\nTime interval: [0,88]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 1546, number of events= 1546 
   (908 observations deleted due to missingness)

                                  coef exp(coef) se(coef) robust se     z
tr_outcome.lag                 0.23134   1.26029  0.09190   0.07725  2.99
less_90d_tr1.lag               0.95955   2.61051  0.08825   0.08096 11.85
log_dias_treat_imp_sin_na.lag  0.09043   1.09465  0.05335   0.04257  2.12
comp_bpsc_y3_severe.lag        0.31352   1.36824  0.06024   0.05549  5.65
policonsumo2.lag              -0.08198   0.92129  0.08184   0.06548 -1.25
edad_al_ing_1                  0.12309   1.13099  0.01311   0.01174 10.49
ano_nac_corr                   0.12241   1.13022  0.01264   0.01153 10.61
susinidum_oh                  -0.19596   0.82204  0.17904   0.17621 -1.11
susinidum_coc                  0.00617   1.00619  0.20987   0.20957  0.03
susinidum_pbc                 -0.06247   0.93945  0.19053   0.18657 -0.33
susinidum_mar                 -0.06877   0.93355  0.18001   0.17810 -0.39
psycom_dum_with               -0.05898   0.94272  0.05788   0.05379 -1.10
psycom_dum_study              -0.47231   0.62356  0.08757   0.09703 -4.87
freq_cons_dum_5day            -0.03117   0.96931  0.05294   0.05513 -0.57
cond_oc_dum_2inact             0.11950   1.12693  0.06683   0.06368  1.88
cond_oc_dum_3unemp             0.02634   1.02669  0.06935   0.07437  0.35
susprindum_oh                 -0.09921   0.90555  0.19340   0.19563 -0.51
susprindum_coc                 0.01020   1.01025  0.19593   0.21054  0.05
susprindum_pbc                -0.07461   0.92811  0.18970   0.20223 -0.37
susprindum_mar                -0.08083   0.92235  0.20716   0.21899 -0.37
                              Pr(>|z|)    
tr_outcome.lag                  0.0027 ** 
less_90d_tr1.lag               < 2e-16 ***
log_dias_treat_imp_sin_na.lag   0.0336 *  
comp_bpsc_y3_severe.lag        1.6e-08 ***
policonsumo2.lag                0.2106    
edad_al_ing_1                  < 2e-16 ***
ano_nac_corr                   < 2e-16 ***
susinidum_oh                    0.2661    
susinidum_coc                   0.9765    
susinidum_pbc                   0.7378    
susinidum_mar                   0.6994    
psycom_dum_with                 0.2729    
psycom_dum_study               1.1e-06 ***
freq_cons_dum_5day              0.5718    
cond_oc_dum_2inact              0.0606 .  
cond_oc_dum_3unemp              0.7232    
susprindum_oh                   0.6121    
susprindum_coc                  0.9614    
susprindum_pbc                  0.7122    
susprindum_mar                  0.7121    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    1.260      0.793     1.083     1.466
less_90d_tr1.lag                  2.611      0.383     2.227     3.059
log_dias_treat_imp_sin_na.lag     1.095      0.914     1.007     1.190
comp_bpsc_y3_severe.lag           1.368      0.731     1.227     1.525
policonsumo2.lag                  0.921      1.085     0.810     1.047
edad_al_ing_1                     1.131      0.884     1.105     1.157
ano_nac_corr                      1.130      0.885     1.105     1.156
susinidum_oh                      0.822      1.216     0.582     1.161
susinidum_coc                     1.006      0.994     0.667     1.517
susinidum_pbc                     0.939      1.064     0.652     1.354
susinidum_mar                     0.934      1.071     0.658     1.324
psycom_dum_with                   0.943      1.061     0.848     1.048
psycom_dum_study                  0.624      1.604     0.516     0.754
freq_cons_dum_5day                0.969      1.032     0.870     1.080
cond_oc_dum_2inact                1.127      0.887     0.995     1.277
cond_oc_dum_3unemp                1.027      0.974     0.887     1.188
susprindum_oh                     0.906      1.104     0.617     1.329
susprindum_coc                    1.010      0.990     0.669     1.526
susprindum_pbc                    0.928      1.077     0.624     1.380
susprindum_mar                    0.922      1.084     0.600     1.417

Concordance= 0.659  (se = 0.006 )
Likelihood ratio test= 396  on 20 df,   p=<2e-16
Wald test            = 391  on 20 df,   p=<2e-16
Score (logrank) test = 404  on 20 df,   p=<2e-16,   Robust = 375  p=<2e-16

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.134   0.312   0.489   0.630   0.868   2.768 
[1] "Modality: WO residential\nTime interval: (93,135]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 34, number of events= 34 
   (32 observations deleted due to missingness)

                                 coef exp(coef) se(coef) robust se     z
tr_outcome.lag                     NA        NA   0.0000    0.0000    NA
less_90d_tr1.lag               4.4157   82.7386   2.7593    2.7900  1.58
log_dias_treat_imp_sin_na.lag  4.4195   83.0584   2.1191    1.9017  2.32
comp_bpsc_y3_severe.lag            NA        NA   0.0000    0.0000    NA
policonsumo2.lag                   NA        NA   0.0000    0.0000    NA
edad_al_ing_1                  2.4852   12.0039   0.7005    0.8291  3.00
ano_nac_corr                   2.3745   10.7460   0.6782    0.8038  2.95
susinidum_oh                  -4.3298    0.0132   1.6380    1.3810 -3.14
susinidum_coc                      NA        NA   0.0000    0.0000    NA
susinidum_pbc                 -4.5556    0.0105   1.6927    1.5403 -2.96
susinidum_mar                 -4.0816    0.0169   1.5315    1.0705 -3.81
psycom_dum_with                0.3149    1.3702   0.6251    0.5934  0.53
psycom_dum_study               1.8290    6.2277   1.2291    1.1187  1.63
freq_cons_dum_5day            -0.2795    0.7561   0.5600    0.6164 -0.45
cond_oc_dum_2inact             0.6222    1.8630   1.2392    1.7717  0.35
cond_oc_dum_3unemp             1.1566    3.1792   1.1545    1.6923  0.68
susprindum_oh                 -1.6687    0.1885   1.9275    1.7284 -0.97
susprindum_coc                -0.5048    0.6036   1.4589    1.0889 -0.46
susprindum_pbc                -0.5150    0.5975   1.2052    0.7966 -0.65
susprindum_mar                     NA        NA   0.0000    0.0000    NA
                              Pr(>|z|)    
tr_outcome.lag                      NA    
less_90d_tr1.lag               0.11349    
log_dias_treat_imp_sin_na.lag  0.02013 *  
comp_bpsc_y3_severe.lag             NA    
policonsumo2.lag                    NA    
edad_al_ing_1                  0.00272 ** 
ano_nac_corr                   0.00313 ** 
susinidum_oh                   0.00172 ** 
susinidum_coc                       NA    
susinidum_pbc                  0.00310 ** 
susinidum_mar                  0.00014 ***
psycom_dum_with                0.59557    
psycom_dum_study               0.10206    
freq_cons_dum_5day             0.65023    
cond_oc_dum_2inact             0.72545    
cond_oc_dum_3unemp             0.49430    
susprindum_oh                  0.33431    
susprindum_coc                 0.64295    
susprindum_pbc                 0.51798    
susprindum_mar                      NA    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                       NA         NA        NA        NA
less_90d_tr1.lag                82.7386     0.0121  0.349036  1.96e+04
log_dias_treat_imp_sin_na.lag   83.0584     0.0120  1.998148  3.45e+03
comp_bpsc_y3_severe.lag              NA         NA        NA        NA
policonsumo2.lag                     NA         NA        NA        NA
edad_al_ing_1                   12.0039     0.0833  2.363494  6.10e+01
ano_nac_corr                    10.7460     0.0931  2.223632  5.19e+01
susinidum_oh                     0.0132    75.9321  0.000879  1.97e-01
susinidum_coc                        NA         NA        NA        NA
susinidum_pbc                    0.0105    95.1637  0.000513  2.15e-01
susinidum_mar                    0.0169    59.2412  0.002071  1.38e-01
psycom_dum_with                  1.3702     0.7298  0.428266  4.38e+00
psycom_dum_study                 6.2277     0.1606  0.695181  5.58e+01
freq_cons_dum_5day               0.7561     1.3225  0.225893  2.53e+00
cond_oc_dum_2inact               1.8630     0.5368  0.057826  6.00e+01
cond_oc_dum_3unemp               3.1792     0.3145  0.115310  8.77e+01
susprindum_oh                    0.1885     5.3052  0.006370  5.58e+00
susprindum_coc                   0.6036     1.6566  0.071431  5.10e+00
susprindum_pbc                   0.5975     1.6736  0.125403  2.85e+00
susprindum_mar                       NA         NA        NA        NA

Concordance= 0.777  (se = 0.035 )
Likelihood ratio test= 29.1  on 15 df,   p=0.02
Wald test            = 93.5  on 15 df,   p=2e-13
Score (logrank) test = 36.1  on 15 df,   p=0.002,   Robust = 28.5  p=0.02

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.02    0.19    0.63    1.20    1.94    4.91 
[1] "Modality: GP intensive ambulatory\nTime interval: (93,135]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 213, number of events= 213 
   (202 observations deleted due to missingness)

                                 coef exp(coef) se(coef) robust se     z
tr_outcome.lag                -0.1420    0.8676   0.9397    0.6795 -0.21
less_90d_tr1.lag               0.1767    1.1933   1.2881    0.9068  0.19
log_dias_treat_imp_sin_na.lag -0.6701    0.5116   0.9782    0.9147 -0.73
comp_bpsc_y3_severe.lag       -0.0543    0.9471   0.6691    0.5533 -0.10
policonsumo2.lag              -0.4395    0.6444   0.8116    0.5030 -0.87
edad_al_ing_1                  0.7008    2.0153   0.1090    0.1087  6.45
ano_nac_corr                   0.6961    2.0059   0.1104    0.1105  6.30
susinidum_oh                  -0.0531    0.9483   0.3644    0.3947 -0.13
susinidum_coc                  0.0278    1.0282   0.5261    0.4879  0.06
susinidum_pbc                  0.0657    1.0679   0.5213    0.6393  0.10
susinidum_mar                 -0.0596    0.9421   0.3790    0.4164 -0.14
psycom_dum_with               -0.1269    0.8808   0.1630    0.1558 -0.81
psycom_dum_study              -0.1664    0.8467   0.2567    0.2651 -0.63
freq_cons_dum_5day             0.2489    1.2826   0.1656    0.1562  1.59
cond_oc_dum_2inact            -0.0389    0.9619   0.2675    0.2474 -0.16
cond_oc_dum_3unemp            -0.0623    0.9396   0.1663    0.1661 -0.38
susprindum_oh                 -0.8567    0.4246   0.6738    0.5892 -1.45
susprindum_coc                -0.9290    0.3950   0.6812    0.6043 -1.54
susprindum_pbc                -0.5742    0.5631   0.6592    0.5839 -0.98
susprindum_mar                -0.7788    0.4590   0.7109    0.6565 -1.19
                              Pr(>|z|)    
tr_outcome.lag                    0.83    
less_90d_tr1.lag                  0.85    
log_dias_treat_imp_sin_na.lag     0.46    
comp_bpsc_y3_severe.lag           0.92    
policonsumo2.lag                  0.38    
edad_al_ing_1                  1.1e-10 ***
ano_nac_corr                   3.0e-10 ***
susinidum_oh                      0.89    
susinidum_coc                     0.95    
susinidum_pbc                     0.92    
susinidum_mar                     0.89    
psycom_dum_with                   0.42    
psycom_dum_study                  0.53    
freq_cons_dum_5day                0.11    
cond_oc_dum_2inact                0.88    
cond_oc_dum_3unemp                0.71    
susprindum_oh                     0.15    
susprindum_coc                    0.12    
susprindum_pbc                    0.33    
susprindum_mar                    0.24    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    0.868      1.153    0.2290      3.29
less_90d_tr1.lag                  1.193      0.838    0.2018      7.06
log_dias_treat_imp_sin_na.lag     0.512      1.955    0.0852      3.07
comp_bpsc_y3_severe.lag           0.947      1.056    0.3202      2.80
policonsumo2.lag                  0.644      1.552    0.2404      1.73
edad_al_ing_1                     2.015      0.496    1.6287      2.49
ano_nac_corr                      2.006      0.499    1.6152      2.49
susinidum_oh                      0.948      1.054    0.4375      2.06
susinidum_coc                     1.028      0.973    0.3951      2.68
susinidum_pbc                     1.068      0.936    0.3051      3.74
susinidum_mar                     0.942      1.061    0.4165      2.13
psycom_dum_with                   0.881      1.135    0.6490      1.20
psycom_dum_study                  0.847      1.181    0.5036      1.42
freq_cons_dum_5day                1.283      0.780    0.9443      1.74
cond_oc_dum_2inact                0.962      1.040    0.5923      1.56
cond_oc_dum_3unemp                0.940      1.064    0.6785      1.30
susprindum_oh                     0.425      2.355    0.1338      1.35
susprindum_coc                    0.395      2.532    0.1208      1.29
susprindum_pbc                    0.563      1.776    0.1793      1.77
susprindum_mar                    0.459      2.179    0.1268      1.66

Concordance= 0.648  (se = 0.021 )
Likelihood ratio test= 62.5  on 20 df,   p=3e-06
Wald test            = 74.1  on 20 df,   p=4e-08
Score (logrank) test = 58  on 20 df,   p=1e-05,   Robust = 60.1  p=7e-06

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.57    2.31    3.29    4.18    4.76   29.49 
[1] "Modality: basic ambulatory\nTime interval: (93,135]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 200, number of events= 200 
   (189 observations deleted due to missingness)

                                 coef exp(coef) se(coef) robust se     z
tr_outcome.lag                -0.7197    0.4869   1.2028    0.3990 -1.80
less_90d_tr1.lag              -1.8129    0.1632   1.7882    1.2190 -1.49
log_dias_treat_imp_sin_na.lag -1.8079    0.1640   1.0549    0.8626 -2.10
comp_bpsc_y3_severe.lag        0.0974    1.1023   1.1447    0.4256  0.23
policonsumo2.lag               2.2023    9.0461   1.9113    1.0858  2.03
edad_al_ing_1                  0.7794    2.1801   0.1172    0.1249  6.24
ano_nac_corr                   0.7680    2.1554   0.1169    0.1245  6.17
susinidum_oh                   0.5302    1.6993   0.5270    0.3807  1.39
susinidum_coc                  0.1994    1.2206   0.5865    0.4163  0.48
susinidum_pbc                  0.6580    1.9309   0.5718    0.4256  1.55
susinidum_mar                  0.7490    2.1148   0.5406    0.3995  1.87
psycom_dum_with                0.0195    1.0197   0.1761    0.1662  0.12
psycom_dum_study               0.0389    1.0397   0.2107    0.2057  0.19
freq_cons_dum_5day             0.2773    1.3195   0.1653    0.1702  1.63
cond_oc_dum_2inact             0.2667    1.3057   0.2393    0.2809  0.95
cond_oc_dum_3unemp            -0.0170    0.9831   0.1848    0.1678 -0.10
susprindum_oh                 -0.8348    0.4340   0.5167    0.4450 -1.88
susprindum_coc                -0.8892    0.4110   0.5240    0.4586 -1.94
susprindum_pbc                -0.7822    0.4574   0.5047    0.4376 -1.79
susprindum_mar                -0.3340    0.7161   0.5473    0.5222 -0.64
                              Pr(>|z|)    
tr_outcome.lag                   0.071 .  
less_90d_tr1.lag                 0.137    
log_dias_treat_imp_sin_na.lag    0.036 *  
comp_bpsc_y3_severe.lag          0.819    
policonsumo2.lag                 0.043 *  
edad_al_ing_1                  4.3e-10 ***
ano_nac_corr                   7.0e-10 ***
susinidum_oh                     0.164    
susinidum_coc                    0.632    
susinidum_pbc                    0.122    
susinidum_mar                    0.061 .  
psycom_dum_with                  0.907    
psycom_dum_study                 0.850    
freq_cons_dum_5day               0.103    
cond_oc_dum_2inact               0.342    
cond_oc_dum_3unemp               0.919    
susprindum_oh                    0.061 .  
susprindum_coc                   0.052 .  
susprindum_pbc                   0.074 .  
susprindum_mar                   0.522    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    0.487      2.054    0.2228     1.064
less_90d_tr1.lag                  0.163      6.128    0.0150     1.779
log_dias_treat_imp_sin_na.lag     0.164      6.098    0.0302     0.889
comp_bpsc_y3_severe.lag           1.102      0.907    0.4787     2.538
policonsumo2.lag                  9.046      0.111    1.0769    75.986
edad_al_ing_1                     2.180      0.459    1.7068     2.785
ano_nac_corr                      2.155      0.464    1.6885     2.751
susinidum_oh                      1.699      0.588    0.8057     3.584
susinidum_coc                     1.221      0.819    0.5398     2.760
susinidum_pbc                     1.931      0.518    0.8384     4.447
susinidum_mar                     2.115      0.473    0.9666     4.627
psycom_dum_with                   1.020      0.981    0.7361     1.412
psycom_dum_study                  1.040      0.962    0.6948     1.556
freq_cons_dum_5day                1.320      0.758    0.9453     1.842
cond_oc_dum_2inact                1.306      0.766    0.7529     2.264
cond_oc_dum_3unemp                0.983      1.017    0.7075     1.366
susprindum_oh                     0.434      2.304    0.1814     1.038
susprindum_coc                    0.411      2.433    0.1673     1.010
susprindum_pbc                    0.457      2.186    0.1940     1.078
susprindum_mar                    0.716      1.397    0.2573     1.993

Concordance= 0.664  (se = 0.022 )
Likelihood ratio test= 68.2  on 20 df,   p=4e-07
Wald test            = 175  on 20 df,   p=<2e-16
Score (logrank) test = 66.3  on 20 df,   p=7e-07,   Robust = 66.8  p=6e-07

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.24    0.70    1.13    1.58    1.96   11.84 
[1] "Modality: GP residential\nTime interval: (93,135]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 87, number of events= 87 
   (80 observations deleted due to missingness)

                                 coef exp(coef) se(coef) robust se     z
tr_outcome.lag                -2.5530    0.0779   2.0164    0.9029 -2.83
less_90d_tr1.lag               2.0565    7.8187   1.0277    0.5534  3.72
log_dias_treat_imp_sin_na.lag  0.5230    1.6871   1.4091    0.6244  0.84
comp_bpsc_y3_severe.lag       -0.0445    0.9565   1.7827    0.8678 -0.05
policonsumo2.lag              -2.8354    0.0587   2.0626    1.3710 -2.07
edad_al_ing_1                  1.0157    2.7612   0.2224    0.1885  5.39
ano_nac_corr                   1.0052    2.7325   0.2221    0.1868  5.38
susinidum_oh                  -0.9494    0.3870   1.0531    0.3347 -2.84
susinidum_coc                 -0.9051    0.4045   1.2157    0.5140 -1.76
susinidum_pbc                 -1.3499    0.2593   1.1023    0.4586 -2.94
susinidum_mar                 -1.1042    0.3315   1.0590    0.3061 -3.61
psycom_dum_with               -0.0621    0.9398   0.2946    0.2650 -0.23
psycom_dum_study               0.2976    1.3466   0.4129    0.3391  0.88
freq_cons_dum_5day            -0.7801    0.4583   0.2954    0.2557 -3.05
cond_oc_dum_2inact            -0.2235    0.7997   0.5043    0.5206 -0.43
cond_oc_dum_3unemp            -0.5810    0.5594   0.4172    0.4796 -1.21
susprindum_oh                 -3.2829    0.0375   1.2809    0.6779 -4.84
susprindum_coc                -2.3737    0.0931   1.2918    0.8134 -2.92
susprindum_pbc                -2.1977    0.1111   1.2660    0.7348 -2.99
susprindum_mar                -1.6220    0.1975   1.5119    0.7975 -2.03
                              Pr(>|z|)    
tr_outcome.lag                 0.00469 ** 
less_90d_tr1.lag               0.00020 ***
log_dias_treat_imp_sin_na.lag  0.40220    
comp_bpsc_y3_severe.lag        0.95912    
policonsumo2.lag               0.03863 *  
edad_al_ing_1                  7.1e-08 ***
ano_nac_corr                   7.4e-08 ***
susinidum_oh                   0.00457 ** 
susinidum_coc                  0.07827 .  
susinidum_pbc                  0.00324 ** 
susinidum_mar                  0.00031 ***
psycom_dum_with                0.81465    
psycom_dum_study               0.38018    
freq_cons_dum_5day             0.00228 ** 
cond_oc_dum_2inact             0.66772    
cond_oc_dum_3unemp             0.22571    
susprindum_oh                  1.3e-06 ***
susprindum_coc                 0.00352 ** 
susprindum_pbc                 0.00278 ** 
susprindum_mar                 0.04197 *  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                   0.0779     12.845   0.01326     0.457
less_90d_tr1.lag                 7.8187      0.128   2.64266    23.133
log_dias_treat_imp_sin_na.lag    1.6871      0.593   0.49623     5.736
comp_bpsc_y3_severe.lag          0.9565      1.045   0.17460     5.240
policonsumo2.lag                 0.0587     17.037   0.00400     0.862
edad_al_ing_1                    2.7612      0.362   1.90834     3.995
ano_nac_corr                     2.7325      0.366   1.89481     3.941
susinidum_oh                     0.3870      2.584   0.20079     0.746
susinidum_coc                    0.4045      2.472   0.14770     1.108
susinidum_pbc                    0.2593      3.857   0.10554     0.637
susinidum_mar                    0.3315      3.017   0.18192     0.604
psycom_dum_with                  0.9398      1.064   0.55910     1.580
psycom_dum_study                 1.3466      0.743   0.69279     2.617
freq_cons_dum_5day               0.4583      2.182   0.27768     0.757
cond_oc_dum_2inact               0.7997      1.250   0.28831     2.218
cond_oc_dum_3unemp               0.5594      1.788   0.21852     1.432
susprindum_oh                    0.0375     26.654   0.00994     0.142
susprindum_coc                   0.0931     10.737   0.01891     0.459
susprindum_pbc                   0.1111      9.004   0.02631     0.469
susprindum_mar                   0.1975      5.063   0.04138     0.943

Concordance= 0.718  (se = 0.029 )
Likelihood ratio test= 47.9  on 20 df,   p=4e-04
Wald test            = 120  on 20 df,   p=3e-16
Score (logrank) test = 47.1  on 20 df,   p=6e-04,   Robust = 42.4  p=0.002

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
    334    1060    1807    3592    3996   25185 
[1] "Modality: WO intensive ambulatory\nTime interval: (93,135]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 41, number of events= 41 
   (36 observations deleted due to missingness)

                                   coef exp(coef)  se(coef) robust se      z
tr_outcome.lag                 1.20e+01  1.65e+05  4.55e+03  8.85e-01  13.58
less_90d_tr1.lag               2.41e+01  3.00e+10  7.73e+03  2.50e+00   9.64
log_dias_treat_imp_sin_na.lag -2.91e+01  2.33e-13  1.15e+04  2.34e+00 -12.44
comp_bpsc_y3_severe.lag       -3.46e+01  9.18e-16  1.26e+04  2.68e+00 -12.94
policonsumo2.lag              -7.42e+01  5.80e-33  2.82e+04  5.70e+00 -13.02
edad_al_ing_1                  3.98e-01  1.49e+00  3.04e-01  3.27e-01   1.22
ano_nac_corr                   3.63e-01  1.44e+00  2.94e-01  3.20e-01   1.14
susinidum_oh                  -1.29e+00  2.76e-01  6.11e-01  7.17e-01  -1.79
susinidum_coc                 -3.32e+00  3.62e-02  1.39e+00  1.02e+00  -3.25
susinidum_pbc                 -1.46e+00  2.32e-01  7.40e-01  8.96e-01  -1.63
susinidum_mar                        NA        NA  0.00e+00  0.00e+00     NA
psycom_dum_with                4.43e-01  1.56e+00  5.68e-01  5.14e-01   0.86
psycom_dum_study              -3.73e-01  6.88e-01  6.21e-01  6.65e-01  -0.56
freq_cons_dum_5day             1.94e+00  6.93e+00  6.29e-01  6.19e-01   3.13
cond_oc_dum_2inact            -1.71e+00  1.80e-01  9.74e-01  1.08e+00  -1.58
cond_oc_dum_3unemp            -1.81e+00  1.63e-01  1.05e+00  1.06e+00  -1.72
susprindum_oh                 -1.79e+00  1.68e-01  1.55e+00  1.52e+00  -1.18
susprindum_coc                -5.81e-01  5.59e-01  1.26e+00  7.71e-01  -0.75
susprindum_pbc                -5.69e-01  5.66e-01  1.18e+00  7.55e-01  -0.75
susprindum_mar                -1.73e+00  1.77e-01  1.50e+00  1.11e+00  -1.56
                              Pr(>|z|)    
tr_outcome.lag                  <2e-16 ***
less_90d_tr1.lag                <2e-16 ***
log_dias_treat_imp_sin_na.lag   <2e-16 ***
comp_bpsc_y3_severe.lag         <2e-16 ***
policonsumo2.lag                <2e-16 ***
edad_al_ing_1                   0.2238    
ano_nac_corr                    0.2561    
susinidum_oh                    0.0727 .  
susinidum_coc                   0.0012 ** 
susinidum_pbc                   0.1026    
susinidum_mar                       NA    
psycom_dum_with                 0.3892    
psycom_dum_study                0.5745    
freq_cons_dum_5day              0.0018 ** 
cond_oc_dum_2inact              0.1142    
cond_oc_dum_3unemp              0.0862 .  
susprindum_oh                   0.2387    
susprindum_coc                  0.4508    
susprindum_pbc                  0.4510    
susprindum_mar                  0.1199    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                 1.65e+05   6.06e-06  2.92e+04  9.35e+05
less_90d_tr1.lag               3.00e+10   3.34e-11  2.22e+08  4.04e+12
log_dias_treat_imp_sin_na.lag  2.33e-13   4.29e+12  2.38e-15  2.28e-11
comp_bpsc_y3_severe.lag        9.18e-16   1.09e+15  4.85e-18  1.74e-13
policonsumo2.lag               5.80e-33   1.72e+32  8.14e-38  4.13e-28
edad_al_ing_1                  1.49e+00   6.72e-01  7.84e-01  2.83e+00
ano_nac_corr                   1.44e+00   6.96e-01  7.68e-01  2.69e+00
susinidum_oh                   2.76e-01   3.62e+00  6.78e-02  1.13e+00
susinidum_coc                  3.62e-02   2.76e+01  4.90e-03  2.68e-01
susinidum_pbc                  2.32e-01   4.31e+00  4.01e-02  1.34e+00
susinidum_mar                        NA         NA        NA        NA
psycom_dum_with                1.56e+00   6.42e-01  5.68e-01  4.27e+00
psycom_dum_study               6.88e-01   1.45e+00  1.87e-01  2.53e+00
freq_cons_dum_5day             6.93e+00   1.44e-01  2.06e+00  2.33e+01
cond_oc_dum_2inact             1.80e-01   5.55e+00  2.15e-02  1.51e+00
cond_oc_dum_3unemp             1.63e-01   6.12e+00  2.06e-02  1.29e+00
susprindum_oh                  1.68e-01   5.96e+00  8.59e-03  3.27e+00
susprindum_coc                 5.59e-01   1.79e+00  1.24e-01  2.53e+00
susprindum_pbc                 5.66e-01   1.77e+00  1.29e-01  2.49e+00
susprindum_mar                 1.77e-01   5.64e+00  2.00e-02  1.57e+00

Concordance= 0.725  (se = 0.05 )
Likelihood ratio test= 24.1  on 19 df,   p=0.2
Wald test            = 941  on 19 df,   p=<2e-16
Score (logrank) test = 26.8  on 19 df,   p=0.1,   Robust = 19.6  p=0.4

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
1.62e+29 1.94e+30 4.51e+30 2.39e+37 9.76e+30 9.82e+38 
[1] "Modality: WO residential\nTime interval: (88,93]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 13, number of events= 13 
   (13 observations deleted due to missingness)

                                    coef  exp(coef)   se(coef)  robust se     z
tr_outcome.lag                        NA         NA   0.00e+00   0.00e+00    NA
less_90d_tr1.lag                      NA         NA   0.00e+00   0.00e+00    NA
log_dias_treat_imp_sin_na.lag         NA         NA   0.00e+00   0.00e+00    NA
comp_bpsc_y3_severe.lag               NA         NA   0.00e+00   0.00e+00    NA
policonsumo2.lag                      NA         NA   0.00e+00   0.00e+00    NA
edad_al_ing_1                  -5.69e+01   2.01e-25   5.83e+02   2.31e+00 -24.7
ano_nac_corr                   -3.70e+01   8.29e-17   2.42e+04   1.94e+00 -19.1
susinidum_oh                    2.40e+02  1.77e+104   3.87e+05   7.01e+00  34.2
susinidum_coc                         NA         NA   0.00e+00   0.00e+00    NA
susinidum_pbc                   1.68e+02   9.89e+72   1.69e+05   5.26e+00  31.9
susinidum_mar                         NA         NA   0.00e+00   0.00e+00    NA
psycom_dum_with                -2.17e+01   3.76e-10   7.26e+04   1.38e+00 -15.7
psycom_dum_study               -1.21e+02   1.81e-53   4.11e+05   5.48e+00 -22.2
freq_cons_dum_5day             -6.09e+01   3.71e-27   2.42e+04   2.26e+00 -26.9
cond_oc_dum_2inact                    NA         NA   0.00e+00   0.00e+00    NA
cond_oc_dum_3unemp                    NA         NA   0.00e+00   0.00e+00    NA
susprindum_oh                   9.63e+01   6.38e+41   1.21e+05   5.27e+00  18.2
susprindum_coc                  1.21e+02   2.66e+52   7.26e+04   3.47e+00  34.8
susprindum_pbc                  1.14e+02   3.44e+49   2.43e+04   4.27e+00  26.7
susprindum_mar                        NA         NA   0.00e+00   0.00e+00    NA
                              Pr(>|z|)    
tr_outcome.lag                      NA    
less_90d_tr1.lag                    NA    
log_dias_treat_imp_sin_na.lag       NA    
comp_bpsc_y3_severe.lag             NA    
policonsumo2.lag                    NA    
edad_al_ing_1                   <2e-16 ***
ano_nac_corr                    <2e-16 ***
susinidum_oh                    <2e-16 ***
susinidum_coc                       NA    
susinidum_pbc                   <2e-16 ***
susinidum_mar                       NA    
psycom_dum_with                 <2e-16 ***
psycom_dum_study                <2e-16 ***
freq_cons_dum_5day              <2e-16 ***
cond_oc_dum_2inact                  NA    
cond_oc_dum_3unemp                  NA    
susprindum_oh                   <2e-16 ***
susprindum_coc                  <2e-16 ***
susprindum_pbc                  <2e-16 ***
susprindum_mar                      NA    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                       NA         NA        NA        NA
less_90d_tr1.lag                     NA         NA        NA        NA
log_dias_treat_imp_sin_na.lag        NA         NA        NA        NA
comp_bpsc_y3_severe.lag              NA         NA        NA        NA
policonsumo2.lag                     NA         NA        NA        NA
edad_al_ing_1                  2.01e-25   4.98e+24  2.19e-27  1.84e-23
ano_nac_corr                   8.29e-17   1.21e+16  1.84e-18  3.73e-15
susinidum_oh                  1.77e+104  5.64e-105  1.92e+98 1.64e+110
susinidum_coc                        NA         NA        NA        NA
susinidum_pbc                  9.89e+72   1.01e-73  3.30e+68  2.97e+77
susinidum_mar                        NA         NA        NA        NA
psycom_dum_with                3.76e-10   2.66e+09  2.52e-11  5.61e-09
psycom_dum_study               1.81e-53   5.53e+52  3.91e-58  8.35e-49
freq_cons_dum_5day             3.71e-27   2.70e+26  4.43e-29  3.11e-25
cond_oc_dum_2inact                   NA         NA        NA        NA
cond_oc_dum_3unemp                   NA         NA        NA        NA
susprindum_oh                  6.38e+41   1.57e-42  2.07e+37  1.97e+46
susprindum_coc                 2.66e+52   3.76e-53  2.98e+49  2.37e+55
susprindum_pbc                 3.44e+49   2.90e-50  7.99e+45  1.48e+53
susprindum_mar                       NA         NA        NA        NA

Concordance= 1  (se = 0 )
Likelihood ratio test= 45.1  on 10 df,   p=2e-06
Wald test            = 1819  on 10 df,   p=<2e-16
Score (logrank) test = 16.9  on 10 df,   p=0.08,   Robust = 12.6  p=0.2

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
0.00e+00 0.00e+00 0.00e+00 2.29e+68 0.00e+00 2.98e+69 
[1] "Modality: GP intensive ambulatory\nTime interval: (88,93]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 74, number of events= 74 
   (74 observations deleted due to missingness)

                                 coef exp(coef) se(coef) robust se     z
tr_outcome.lag                     NA        NA   0.0000    0.0000    NA
less_90d_tr1.lag                   NA        NA   0.0000    0.0000    NA
log_dias_treat_imp_sin_na.lag      NA        NA   0.0000    0.0000    NA
comp_bpsc_y3_severe.lag            NA        NA   0.0000    0.0000    NA
policonsumo2.lag                   NA        NA   0.0000    0.0000    NA
edad_al_ing_1                  0.0225    1.0228   0.1475    0.1616  0.14
ano_nac_corr                   0.0460    1.0471   0.1395    0.1496  0.31
susinidum_oh                  -0.2291    0.7953   0.2985    0.2714 -0.84
susinidum_coc                  0.8542    2.3495   0.5729    0.5836  1.46
susinidum_pbc                 -0.6890    0.5021   0.5586    0.6669 -1.03
susinidum_mar                      NA        NA   0.0000    0.0000    NA
psycom_dum_with                0.3664    1.4425   0.3078    0.2991  1.22
psycom_dum_study               1.1222    3.0717   0.4851    0.4892  2.29
freq_cons_dum_5day             0.2846    1.3292   0.3179    0.2895  0.98
cond_oc_dum_2inact             0.1720    1.1877   0.4223    0.3660  0.47
cond_oc_dum_3unemp             0.4730    1.6048   0.3933    0.3670  1.29
susprindum_oh                 -0.0812    0.9220   0.6170    0.4583 -0.18
susprindum_coc                -0.0655    0.9366   0.6616    0.5184 -0.13
susprindum_pbc                -0.6944    0.4994   0.5289    0.3637 -1.91
susprindum_mar                     NA        NA   0.0000    0.0000    NA
                              Pr(>|z|)  
tr_outcome.lag                      NA  
less_90d_tr1.lag                    NA  
log_dias_treat_imp_sin_na.lag       NA  
comp_bpsc_y3_severe.lag             NA  
policonsumo2.lag                    NA  
edad_al_ing_1                    0.889  
ano_nac_corr                     0.758  
susinidum_oh                     0.399  
susinidum_coc                    0.143  
susinidum_pbc                    0.302  
susinidum_mar                       NA  
psycom_dum_with                  0.221  
psycom_dum_study                 0.022 *
freq_cons_dum_5day               0.326  
cond_oc_dum_2inact               0.638  
cond_oc_dum_3unemp               0.197  
susprindum_oh                    0.859  
susprindum_coc                   0.900  
susprindum_pbc                   0.056 .
susprindum_mar                      NA  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                       NA         NA        NA        NA
less_90d_tr1.lag                     NA         NA        NA        NA
log_dias_treat_imp_sin_na.lag        NA         NA        NA        NA
comp_bpsc_y3_severe.lag              NA         NA        NA        NA
policonsumo2.lag                     NA         NA        NA        NA
edad_al_ing_1                     1.023      0.978     0.745      1.40
ano_nac_corr                      1.047      0.955     0.781      1.40
susinidum_oh                      0.795      1.257     0.467      1.35
susinidum_coc                     2.350      0.426     0.749      7.37
susinidum_pbc                     0.502      1.992     0.136      1.86
susinidum_mar                        NA         NA        NA        NA
psycom_dum_with                   1.443      0.693     0.803      2.59
psycom_dum_study                  3.072      0.326     1.177      8.01
freq_cons_dum_5day                1.329      0.752     0.754      2.34
cond_oc_dum_2inact                1.188      0.842     0.580      2.43
cond_oc_dum_3unemp                1.605      0.623     0.782      3.29
susprindum_oh                     0.922      1.085     0.375      2.26
susprindum_coc                    0.937      1.068     0.339      2.59
susprindum_pbc                    0.499      2.002     0.245      1.02
susprindum_mar                       NA         NA        NA        NA

Concordance= 0.634  (se = 0.034 )
Likelihood ratio test= 13.9  on 13 df,   p=0.4
Wald test            = 23.4  on 13 df,   p=0.04
Score (logrank) test = 14.5  on 13 df,   p=0.3,   Robust = 16.6  p=0.2

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.198   0.615   0.804   0.936   1.277   2.215 
[1] "Modality: basic ambulatory\nTime interval: (88,93]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 77, number of events= 77 
   (76 observations deleted due to missingness)

                                   coef exp(coef)  se(coef) robust se      z
tr_outcome.lag                 1.31e+00  3.70e+00  1.22e+00  5.87e-01   2.23
less_90d_tr1.lag                     NA        NA  0.00e+00  0.00e+00     NA
log_dias_treat_imp_sin_na.lag        NA        NA  0.00e+00  0.00e+00     NA
comp_bpsc_y3_severe.lag              NA        NA  0.00e+00  0.00e+00     NA
policonsumo2.lag                     NA        NA  0.00e+00  0.00e+00     NA
edad_al_ing_1                 -8.12e-02  9.22e-01  1.57e-01  1.39e-01  -0.58
ano_nac_corr                  -1.72e-01  8.42e-01  1.59e-01  1.40e-01  -1.23
susinidum_oh                  -6.05e-01  5.46e-01  6.92e-01  4.73e-01  -1.28
susinidum_coc                 -8.26e-02  9.21e-01  1.05e+00  6.08e-01  -0.14
susinidum_pbc                 -1.81e+00  1.64e-01  8.99e-01  6.97e-01  -2.59
susinidum_mar                 -8.53e-01  4.26e-01  7.36e-01  5.27e-01  -1.62
psycom_dum_with               -3.47e-01  7.07e-01  3.23e-01  2.73e-01  -1.27
psycom_dum_study              -6.93e-01  5.00e-01  3.65e-01  3.41e-01  -2.03
freq_cons_dum_5day            -2.08e-01  8.12e-01  3.02e-01  2.87e-01  -0.73
cond_oc_dum_2inact             7.39e-01  2.09e+00  4.50e-01  4.40e-01   1.68
cond_oc_dum_3unemp             4.68e-01  1.60e+00  3.39e-01  3.44e-01   1.36
susprindum_oh                 -2.10e+01  7.35e-10  3.08e+03  6.52e-01 -32.26
susprindum_coc                -2.08e+01  9.01e-10  3.08e+03  5.09e-01 -40.94
susprindum_pbc                -1.95e+01  3.43e-09  3.08e+03  5.67e-01 -34.39
susprindum_mar                -2.06e+01  1.17e-09  3.08e+03  6.89e-01 -29.86
                              Pr(>|z|)    
tr_outcome.lag                  0.0258 *  
less_90d_tr1.lag                    NA    
log_dias_treat_imp_sin_na.lag       NA    
comp_bpsc_y3_severe.lag             NA    
policonsumo2.lag                    NA    
edad_al_ing_1                   0.5597    
ano_nac_corr                    0.2186    
susinidum_oh                    0.2008    
susinidum_coc                   0.8919    
susinidum_pbc                   0.0095 ** 
susinidum_mar                   0.1056    
psycom_dum_with                 0.2028    
psycom_dum_study                0.0423 *  
freq_cons_dum_5day              0.4680    
cond_oc_dum_2inact              0.0926 .  
cond_oc_dum_3unemp              0.1732    
susprindum_oh                   <2e-16 ***
susprindum_coc                  <2e-16 ***
susprindum_pbc                  <2e-16 ***
susprindum_mar                  <2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                 3.70e+00   2.70e-01  1.17e+00  1.17e+01
less_90d_tr1.lag                     NA         NA        NA        NA
log_dias_treat_imp_sin_na.lag        NA         NA        NA        NA
comp_bpsc_y3_severe.lag              NA         NA        NA        NA
policonsumo2.lag                     NA         NA        NA        NA
edad_al_ing_1                  9.22e-01   1.08e+00  7.02e-01  1.21e+00
ano_nac_corr                   8.42e-01   1.19e+00  6.41e-01  1.11e+00
susinidum_oh                   5.46e-01   1.83e+00  2.16e-01  1.38e+00
susinidum_coc                  9.21e-01   1.09e+00  2.80e-01  3.03e+00
susinidum_pbc                  1.64e-01   6.11e+00  4.17e-02  6.43e-01
susinidum_mar                  4.26e-01   2.35e+00  1.52e-01  1.20e+00
psycom_dum_with                7.07e-01   1.41e+00  4.14e-01  1.21e+00
psycom_dum_study               5.00e-01   2.00e+00  2.56e-01  9.76e-01
freq_cons_dum_5day             8.12e-01   1.23e+00  4.63e-01  1.42e+00
cond_oc_dum_2inact             2.09e+00   4.77e-01  8.85e-01  4.96e+00
cond_oc_dum_3unemp             1.60e+00   6.26e-01  8.14e-01  3.13e+00
susprindum_oh                  7.35e-10   1.36e+09  2.05e-10  2.64e-09
susprindum_coc                 9.01e-10   1.11e+09  3.33e-10  2.44e-09
susprindum_pbc                 3.43e-09   2.91e+08  1.13e-09  1.04e-08
susprindum_mar                 1.17e-09   8.58e+08  3.02e-10  4.50e-09

Concordance= 0.731  (se = 0.03 )
Likelihood ratio test= 44.5  on 16 df,   p=2e-04
Wald test            = 16181  on 16 df,   p=<2e-16
Score (logrank) test = 111  on 16 df,   p=2e-16,   Robust = 37.4  p=0.002

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
0.00e+00 1.97e+08 3.29e+08 5.05e+08 7.07e+08 1.97e+09 
[1] "Modality: GP residential\nTime interval: (88,93]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 44, number of events= 44 
   (44 observations deleted due to missingness)

                                 coef exp(coef) se(coef) robust se     z
tr_outcome.lag                     NA        NA   0.0000    0.0000    NA
less_90d_tr1.lag                   NA        NA   0.0000    0.0000    NA
log_dias_treat_imp_sin_na.lag      NA        NA   0.0000    0.0000    NA
comp_bpsc_y3_severe.lag            NA        NA   0.0000    0.0000    NA
policonsumo2.lag                   NA        NA   0.0000    0.0000    NA
edad_al_ing_1                 -0.0941    0.9102   0.2559    0.2579 -0.36
ano_nac_corr                  -0.1054    0.9000   0.2603    0.2672 -0.39
susinidum_oh                  -1.3805    0.2515   1.1947    0.7912 -1.74
susinidum_coc                 -2.3484    0.0955   1.7688    1.0683 -2.20
susinidum_pbc                 -1.7372    0.1760   1.2633    0.9136 -1.90
susinidum_mar                 -1.7400    0.1755   1.2540    0.9334 -1.86
psycom_dum_with               -0.5855    0.5568   0.4569    0.4924 -1.19
psycom_dum_study              -0.5570    0.5729   0.7513    0.6086 -0.92
freq_cons_dum_5day             0.2100    1.2337   0.4609    0.4900  0.43
cond_oc_dum_2inact             1.6085    4.9952   0.8868    0.6243  2.58
cond_oc_dum_3unemp             0.6958    2.0053   0.6945    0.7280  0.96
susprindum_oh                 -3.2052    0.0406   1.4883    1.2502 -2.56
susprindum_coc                -2.1759    0.1135   1.4852    1.1353 -1.92
susprindum_pbc                -2.7616    0.0632   1.2925    0.9600 -2.88
susprindum_mar                     NA        NA   0.0000    0.0000    NA
                              Pr(>|z|)   
tr_outcome.lag                      NA   
less_90d_tr1.lag                    NA   
log_dias_treat_imp_sin_na.lag       NA   
comp_bpsc_y3_severe.lag             NA   
policonsumo2.lag                    NA   
edad_al_ing_1                    0.715   
ano_nac_corr                     0.693   
susinidum_oh                     0.081 . 
susinidum_coc                    0.028 * 
susinidum_pbc                    0.057 . 
susinidum_mar                    0.062 . 
psycom_dum_with                  0.234   
psycom_dum_study                 0.360   
freq_cons_dum_5day               0.668   
cond_oc_dum_2inact               0.010 **
cond_oc_dum_3unemp               0.339   
susprindum_oh                    0.010 * 
susprindum_coc                   0.055 . 
susprindum_pbc                   0.004 **
susprindum_mar                      NA   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                       NA         NA        NA        NA
less_90d_tr1.lag                     NA         NA        NA        NA
log_dias_treat_imp_sin_na.lag        NA         NA        NA        NA
comp_bpsc_y3_severe.lag              NA         NA        NA        NA
policonsumo2.lag                     NA         NA        NA        NA
edad_al_ing_1                    0.9102      1.099   0.54904     1.509
ano_nac_corr                     0.9000      1.111   0.53305     1.520
susinidum_oh                     0.2515      3.977   0.05333     1.186
susinidum_coc                    0.0955     10.469   0.01177     0.775
susinidum_pbc                    0.1760      5.682   0.02937     1.055
susinidum_mar                    0.1755      5.697   0.02817     1.094
psycom_dum_with                  0.5568      1.796   0.21212     1.462
psycom_dum_study                 0.5729      1.745   0.17380     1.889
freq_cons_dum_5day               1.2337      0.811   0.47219     3.223
cond_oc_dum_2inact               4.9952      0.200   1.46948    16.980
cond_oc_dum_3unemp               2.0053      0.499   0.48140     8.353
susprindum_oh                    0.0406     24.660   0.00350     0.470
susprindum_coc                   0.1135      8.810   0.01227     1.050
susprindum_pbc                   0.0632     15.825   0.00963     0.415
susprindum_mar                       NA         NA        NA        NA

Concordance= 0.62  (se = 0.051 )
Likelihood ratio test= 12.3  on 14 df,   p=0.6
Wald test            = 38.5  on 14 df,   p=4e-04
Score (logrank) test = 20  on 14 df,   p=0.1,   Robust = 11.2  p=0.7

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
    2.6    34.2    48.9    52.1    70.7    99.9 
[1] "Modality: WO intensive ambulatory\nTime interval: (88,93]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 10, number of events= 10 
   (10 observations deleted due to missingness)

                                   coef exp(coef)  se(coef) robust se      z
tr_outcome.lag                       NA        NA  0.00e+00  0.00e+00     NA
less_90d_tr1.lag                     NA        NA  0.00e+00  0.00e+00     NA
log_dias_treat_imp_sin_na.lag        NA        NA  0.00e+00  0.00e+00     NA
comp_bpsc_y3_severe.lag              NA        NA  0.00e+00  0.00e+00     NA
policonsumo2.lag                     NA        NA  0.00e+00  0.00e+00     NA
edad_al_ing_1                 -5.34e+01  6.69e-24  6.64e+03  1.45e+00 -36.92
ano_nac_corr                  -4.99e+01  2.05e-22  5.79e+03  1.40e+00 -35.58
susinidum_oh                   5.70e+00  2.99e+02  1.73e+03  1.07e+00   5.31
susinidum_coc                        NA        NA  0.00e+00  0.00e+00     NA
susinidum_pbc                  5.46e+01  5.41e+23  1.65e+04  3.47e+00  15.75
susinidum_mar                        NA        NA  0.00e+00  0.00e+00     NA
psycom_dum_with                2.74e+01  8.14e+11  4.34e+03  1.45e+00  18.95
psycom_dum_study                     NA        NA  0.00e+00  0.00e+00     NA
freq_cons_dum_5day            -1.48e+02  6.27e-65  1.42e+04  3.58e+00 -41.25
cond_oc_dum_2inact            -4.10e+01  1.49e-18  8.48e+03  2.02e+00 -20.30
cond_oc_dum_3unemp                   NA        NA  0.00e+00  0.00e+00     NA
susprindum_oh                  7.39e+01  1.23e+32  6.98e+03  3.27e+00  22.57
susprindum_coc                 1.39e+02  1.89e+60  6.56e+03  3.67e+00  37.84
susprindum_pbc                       NA        NA  0.00e+00  0.00e+00     NA
susprindum_mar                       NA        NA  0.00e+00  0.00e+00     NA
                              Pr(>|z|)    
tr_outcome.lag                      NA    
less_90d_tr1.lag                    NA    
log_dias_treat_imp_sin_na.lag       NA    
comp_bpsc_y3_severe.lag             NA    
policonsumo2.lag                    NA    
edad_al_ing_1                  < 2e-16 ***
ano_nac_corr                   < 2e-16 ***
susinidum_oh                   1.1e-07 ***
susinidum_coc                       NA    
susinidum_pbc                  < 2e-16 ***
susinidum_mar                       NA    
psycom_dum_with                < 2e-16 ***
psycom_dum_study                    NA    
freq_cons_dum_5day             < 2e-16 ***
cond_oc_dum_2inact             < 2e-16 ***
cond_oc_dum_3unemp                  NA    
susprindum_oh                  < 2e-16 ***
susprindum_coc                 < 2e-16 ***
susprindum_pbc                      NA    
susprindum_mar                      NA    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                       NA         NA        NA        NA
less_90d_tr1.lag                     NA         NA        NA        NA
log_dias_treat_imp_sin_na.lag        NA         NA        NA        NA
comp_bpsc_y3_severe.lag              NA         NA        NA        NA
policonsumo2.lag                     NA         NA        NA        NA
edad_al_ing_1                  6.69e-24   1.50e+23  3.94e-25  1.14e-22
ano_nac_corr                   2.05e-22   4.87e+21  1.31e-23  3.21e-21
susinidum_oh                   2.99e+02   3.34e-03  3.64e+01  2.45e+03
susinidum_coc                        NA         NA        NA        NA
susinidum_pbc                  5.41e+23   1.85e-24  6.03e+20  4.86e+26
susinidum_mar                        NA         NA        NA        NA
psycom_dum_with                8.14e+11   1.23e-12  4.77e+10  1.39e+13
psycom_dum_study                     NA         NA        NA        NA
freq_cons_dum_5day             6.27e-65   1.59e+64  5.58e-68  7.05e-62
cond_oc_dum_2inact             1.49e-18   6.70e+17  2.84e-20  7.85e-17
cond_oc_dum_3unemp                   NA         NA        NA        NA
susprindum_oh                  1.23e+32   8.13e-33  2.01e+29  7.53e+34
susprindum_coc                 1.89e+60   5.28e-61  1.43e+57  2.51e+63
susprindum_pbc                       NA         NA        NA        NA
susprindum_mar                       NA         NA        NA        NA

Concordance= 1  (se = 0 )
Likelihood ratio test= 30.2  on 9 df,   p=4e-04
Wald test            = 1857  on 9 df,   p=<2e-16
Score (logrank) test = 21.2  on 9 df,   p=0.01,   Robust = 9.99  p=0.4

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
0.00e+00 0.00e+00 8.00e+01 1.71e+24 2.95e+13 1.71e+25 
Code
# Warning messages:
# 1: In Surv(time.lag, time, event) :
#   Stop time must be > start time, NA created
# 2: In Surv(time.lag, time, event) :
#   Stop time must be > start time, NA created
# 3: In Surv(time.lag, time, event) :
#   Stop time must be > start time, NA created
# 4: In Surv(time.lag, time, event) :
#   Stop time must be > start time, NA created
# 5: In Surv(time.lag, time, event) :
#   Stop time must be > start time, NA created
# 6: In agreg.fit(X, Y, istrat, offset, init, control, weights = weights,  :
#   Loglik converged before variable  1,2,3,4,5 ; beta may be infinite. 
# 7: In agreg.fit(X, Y, istrat, offset, init, control, weights = weights,  :
#   Ran out of iterations and did not converge
# 8: In agreg.fit(X, Y, istrat, offset, init, control, weights = weights,  :
#   Loglik converged before variable  17,18,19,20 ; beta may be infinite. 
# 9: In agreg.fit(X, Y, istrat, offset, init, control, weights = weights,  :
#   Ran out of iterations and did not converge

#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:
iiws_strat_alt <- list()

comb_strata_alt <- expand.grid(unique(data_mine_miss_restr_proc2$tipo_de_plan_2_mod), 
                           unique(data_mine_miss_restr_proc2$time_interval3_alt))

for (ff in seq_along(1:nrow(comb_strata_alt))) {
  i<- comb_strata_alt[ff,"Var1"]
  j<- comb_strata_alt[ff,"Var2"]
  data_mine_miss_proc2_subset<-
  subset(data_mine_miss_restr_proc2, subset= tipo_de_plan_2_mod==i & time_interval3_alt==j)
  maxfu_df_subset <- subset(maxfu_restr_df, subset= maxfu.id %in% unique(data_mine_miss_proc2_subset$id))  
  iiw_model_strat_alt<-
    IrregLong::iiw.weights(Surv(time.lag,time,event)~ 
          cluster(id)+ #If a frailty model is used, the cluster(id) term should appear before other covariates
          tr_outcome.lag+
          less_90d_tr1.lag+
          log_dias_treat_imp_sin_na.lag +
          comp_bpsc_y3_severe.lag+
          policonsumo2.lag + 
          edad_al_ing_1 + 
          ano_nac_corr + 
          susinidum_oh +
          susinidum_coc +
          susinidum_pbc +
          susinidum_mar +
          psycom_dum_with +
          psycom_dum_study + 
          freq_cons_dum_5day +
          cond_oc_dum_2inact +
          cond_oc_dum_3unemp +
          susprindum_oh +
          susprindum_coc +
          susprindum_pbc +
          susprindum_mar, 
      data= data_mine_miss_proc2_subset,
      id= "id",
      time= "time",
      event= "event", #character string indicating which column of the data indicates whether or not a visit occurred. If every row corresponds to a visit, then this column will consist entirely of ones
      maxfu= maxfu_df_subset,
      invariant= c("edad_al_ing_1", "ano_nac_corr", "susinidum_oh", "susinidum_coc", "susinidum_pbc", "susinidum_mar",  "psycom_dum_with", "psycom_dum_study_rec2", "freq_cons_dum_5day", "cond_oc_dum_3unemp", "cond_oc_dum_2inact", "susprindum_oh", "susprindum_coc", "susprindum_pbc", "susprindum_mar"),
      lagvars= c("time", "tr_outcome","log_dias_treat_imp_sin_na", "less_90d_tr1","comp_bpsc_y3_severe", "policonsumo2"),
      lagfirst= c(2.95082,0,4.499811/2,0,0,0),  #90/30.5 4.499811 es 90 días
      first= T
)
  
  iiws_strat_alt[[ff]] <- list(
      model = iiw_model_strat_alt,
      comb = paste0("Modality: ",i,"\nTime interval: ", j,"."),
      cox_iiw = summary(iiw_model_strat_alt$m),
      iiw = summary(iiw_model_strat_alt$iiw)
  )
  print(paste0("Modality: ",i,"\nTime interval: ", j))
  print(summary(iiw_model_strat_alt$m))
  print(summary(iiw_model_strat_alt$iiw))

}
[1] "Modality: WO residential\nTime interval: [0,10]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 613, number of events= 613 
   (1197 observations deleted due to missingness)

                                 coef exp(coef) se(coef) robust se     z
tr_outcome.lag                -0.2763    0.7586   0.2286    0.1902 -1.45
less_90d_tr1.lag              -0.0115    0.9886   0.2375    0.2083 -0.06
log_dias_treat_imp_sin_na.lag -0.2477    0.7806   0.0793    0.0698 -3.55
comp_bpsc_y3_severe.lag        0.3801    1.4625   0.1943    0.1801  2.11
policonsumo2.lag              -0.2659    0.7665   0.1877    0.1779 -1.49
edad_al_ing_1                 -0.0252    0.9751   0.0169    0.0164 -1.54
ano_nac_corr                  -0.0215    0.9788   0.0163    0.0159 -1.35
susinidum_oh                  -0.0912    0.9128   0.2942    0.2869 -0.32
susinidum_coc                 -0.4372    0.6459   0.3413    0.3259 -1.34
susinidum_pbc                  0.2467    1.2797   0.3006    0.3001  0.82
susinidum_mar                  0.1396    1.1498   0.2900    0.2825  0.49
psycom_dum_with               -0.2291    0.7952   0.1183    0.1149 -1.99
psycom_dum_study              -0.0545    0.9469   0.1764    0.1707 -0.32
freq_cons_dum_5day             0.0366    1.0372   0.0990    0.0965  0.38
cond_oc_dum_2inact             0.1879    1.2068   0.1541    0.1460  1.29
cond_oc_dum_3unemp             0.1970    1.2177   0.1536    0.1476  1.33
susprindum_oh                  0.4527    1.5725   0.2877    0.2560  1.77
susprindum_coc                 0.5399    1.7158   0.2897    0.2441  2.21
susprindum_pbc                 0.4500    1.5683   0.2700    0.2291  1.96
susprindum_mar                 0.5777    1.7820   0.4281    0.4151  1.39
                              Pr(>|z|)    
tr_outcome.lag                 0.14636    
less_90d_tr1.lag               0.95610    
log_dias_treat_imp_sin_na.lag  0.00039 ***
comp_bpsc_y3_severe.lag        0.03478 *  
policonsumo2.lag               0.13504    
edad_al_ing_1                  0.12342    
ano_nac_corr                   0.17773    
susinidum_oh                   0.75058    
susinidum_coc                  0.17976    
susinidum_pbc                  0.41111    
susinidum_mar                  0.62121    
psycom_dum_with                0.04608 *  
psycom_dum_study               0.74937    
freq_cons_dum_5day             0.70495    
cond_oc_dum_2inact             0.19799    
cond_oc_dum_3unemp             0.18196    
susprindum_oh                  0.07699 .  
susprindum_coc                 0.02700 *  
susprindum_pbc                 0.04950 *  
susprindum_mar                 0.16393    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    0.759      1.318     0.522     1.101
less_90d_tr1.lag                  0.989      1.012     0.657     1.487
log_dias_treat_imp_sin_na.lag     0.781      1.281     0.681     0.895
comp_bpsc_y3_severe.lag           1.462      0.684     1.028     2.082
policonsumo2.lag                  0.766      1.305     0.541     1.086
edad_al_ing_1                     0.975      1.026     0.944     1.007
ano_nac_corr                      0.979      1.022     0.949     1.010
susinidum_oh                      0.913      1.095     0.520     1.602
susinidum_coc                     0.646      1.548     0.341     1.223
susinidum_pbc                     1.280      0.781     0.711     2.304
susinidum_mar                     1.150      0.870     0.661     2.000
psycom_dum_with                   0.795      1.257     0.635     0.996
psycom_dum_study                  0.947      1.056     0.678     1.323
freq_cons_dum_5day                1.037      0.964     0.858     1.253
cond_oc_dum_2inact                1.207      0.829     0.906     1.607
cond_oc_dum_3unemp                1.218      0.821     0.912     1.626
susprindum_oh                     1.573      0.636     0.952     2.597
susprindum_coc                    1.716      0.583     1.063     2.769
susprindum_pbc                    1.568      0.638     1.001     2.457
susprindum_mar                    1.782      0.561     0.790     4.020

Concordance= 0.608  (se = 0.012 )
Likelihood ratio test= 72.3  on 20 df,   p=8e-08
Wald test            = 87  on 20 df,   p=2e-10
Score (logrank) test = 67.8  on 20 df,   p=4e-07,   Robust = 76.9  p=1e-08

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.262   0.437   0.516   0.595   0.664   2.194 
[1] "Modality: GP intensive ambulatory\nTime interval: [0,10]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 3052, number of events= 3052 
   (5099 observations deleted due to missingness)

                                  coef exp(coef) se(coef) robust se     z
tr_outcome.lag                -0.54850   0.57781  0.12658   0.11161 -4.91
less_90d_tr1.lag              -0.17418   0.84015  0.10418   0.08989 -1.94
log_dias_treat_imp_sin_na.lag -0.02676   0.97359  0.03060   0.02464 -1.09
comp_bpsc_y3_severe.lag        0.11310   1.11975  0.09735   0.08271  1.37
policonsumo2.lag              -0.17858   0.83646  0.11209   0.09112 -1.96
edad_al_ing_1                  0.01697   1.01712  0.00806   0.00839  2.02
ano_nac_corr                   0.02121   1.02144  0.00792   0.00832  2.55
susinidum_oh                   0.00619   1.00621  0.13394   0.12706  0.05
susinidum_coc                  0.05436   1.05587  0.15881   0.15167  0.36
susinidum_pbc                  0.22952   1.25800  0.14861   0.14520  1.58
susinidum_mar                  0.06074   1.06262  0.13516   0.12913  0.47
psycom_dum_with               -0.06635   0.93580  0.03944   0.04029 -1.65
psycom_dum_study               0.49221   1.63593  0.06861   0.07433  6.62
freq_cons_dum_5day             0.06258   1.06458  0.03765   0.03859  1.62
cond_oc_dum_2inact             0.01356   1.01365  0.05712   0.05738  0.24
cond_oc_dum_3unemp             0.02065   1.02086  0.04043   0.04053  0.51
susprindum_oh                  0.13495   1.14448  0.18044   0.18571  0.73
susprindum_coc                 0.10853   1.11463  0.18058   0.18762  0.58
susprindum_pbc                 0.18824   1.20712  0.17630   0.18498  1.02
susprindum_mar                 0.11757   1.12476  0.19016   0.20048  0.59
                              Pr(>|z|)    
tr_outcome.lag                 8.9e-07 ***
less_90d_tr1.lag                 0.053 .  
log_dias_treat_imp_sin_na.lag    0.277    
comp_bpsc_y3_severe.lag          0.171    
policonsumo2.lag                 0.050 .  
edad_al_ing_1                    0.043 *  
ano_nac_corr                     0.011 *  
susinidum_oh                     0.961    
susinidum_coc                    0.720    
susinidum_pbc                    0.114    
susinidum_mar                    0.638    
psycom_dum_with                  0.100 .  
psycom_dum_study               3.6e-11 ***
freq_cons_dum_5day               0.105    
cond_oc_dum_2inact               0.813    
cond_oc_dum_3unemp               0.610    
susprindum_oh                    0.467    
susprindum_coc                   0.563    
susprindum_pbc                   0.309    
susprindum_mar                   0.558    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    0.578      1.731     0.464     0.719
less_90d_tr1.lag                  0.840      1.190     0.704     1.002
log_dias_treat_imp_sin_na.lag     0.974      1.027     0.928     1.022
comp_bpsc_y3_severe.lag           1.120      0.893     0.952     1.317
policonsumo2.lag                  0.836      1.196     0.700     1.000
edad_al_ing_1                     1.017      0.983     1.001     1.034
ano_nac_corr                      1.021      0.979     1.005     1.038
susinidum_oh                      1.006      0.994     0.784     1.291
susinidum_coc                     1.056      0.947     0.784     1.421
susinidum_pbc                     1.258      0.795     0.946     1.672
susinidum_mar                     1.063      0.941     0.825     1.369
psycom_dum_with                   0.936      1.069     0.865     1.013
psycom_dum_study                  1.636      0.611     1.414     1.893
freq_cons_dum_5day                1.065      0.939     0.987     1.148
cond_oc_dum_2inact                1.014      0.987     0.906     1.134
cond_oc_dum_3unemp                1.021      0.980     0.943     1.105
susprindum_oh                     1.144      0.874     0.795     1.647
susprindum_coc                    1.115      0.897     0.772     1.610
susprindum_pbc                    1.207      0.828     0.840     1.735
susprindum_mar                    1.125      0.889     0.759     1.666

Concordance= 0.592  (se = 0.006 )
Likelihood ratio test= 207  on 20 df,   p=<2e-16
Wald test            = 210  on 20 df,   p=<2e-16
Score (logrank) test = 192  on 20 df,   p=<2e-16,   Robust = 186  p=<2e-16

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.334   0.660   0.799   0.801   0.886   2.476 
[1] "Modality: basic ambulatory\nTime interval: [0,10]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 2876, number of events= 2876 
   (4323 observations deleted due to missingness)

                                  coef exp(coef) se(coef) robust se     z
tr_outcome.lag                -0.49603   0.60894  0.13196   0.14634 -3.39
less_90d_tr1.lag              -0.07117   0.93130  0.11746   0.10758 -0.66
log_dias_treat_imp_sin_na.lag -0.09772   0.90691  0.03669   0.04837 -2.02
comp_bpsc_y3_severe.lag        0.09989   1.10505  0.14866   0.12281  0.81
policonsumo2.lag              -0.22168   0.80117  0.11743   0.10716 -2.07
edad_al_ing_1                  0.04360   1.04456  0.00877   0.00930  4.69
ano_nac_corr                   0.05061   1.05191  0.00855   0.00919  5.51
susinidum_oh                   0.18366   1.20161  0.14648   0.20612  0.89
susinidum_coc                  0.36574   1.44158  0.16677   0.22316  1.64
susinidum_pbc                  0.26939   1.30917  0.16351   0.21714  1.24
susinidum_mar                  0.28065   1.32399  0.14722   0.20772  1.35
psycom_dum_with               -0.05230   0.94904  0.04144   0.04240 -1.23
psycom_dum_study               0.54576   1.72591  0.06099   0.06806  8.02
freq_cons_dum_5day             0.07322   1.07597  0.04307   0.04502  1.63
cond_oc_dum_2inact             0.09844   1.10345  0.06205   0.06203  1.59
cond_oc_dum_3unemp             0.04895   1.05017  0.04549   0.04765  1.03
susprindum_oh                  0.46466   1.59147  0.22518   0.21199  2.19
susprindum_coc                 0.55034   1.73384  0.22608   0.21374  2.57
susprindum_pbc                 0.48029   1.61655  0.22343   0.21187  2.27
susprindum_mar                 0.42294   1.52644  0.23245   0.21795  1.94
                              Pr(>|z|)    
tr_outcome.lag                  0.0007 ***
less_90d_tr1.lag                0.5083    
log_dias_treat_imp_sin_na.lag   0.0434 *  
comp_bpsc_y3_severe.lag         0.4160    
policonsumo2.lag                0.0386 *  
edad_al_ing_1                  2.8e-06 ***
ano_nac_corr                   3.6e-08 ***
susinidum_oh                    0.3729    
susinidum_coc                   0.1012    
susinidum_pbc                   0.2147    
susinidum_mar                   0.1767    
psycom_dum_with                 0.2174    
psycom_dum_study               1.1e-15 ***
freq_cons_dum_5day              0.1039    
cond_oc_dum_2inact              0.1125    
cond_oc_dum_3unemp              0.3043    
susprindum_oh                   0.0284 *  
susprindum_coc                  0.0100 *  
susprindum_pbc                  0.0234 *  
susprindum_mar                  0.0523 .  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    0.609      1.642     0.457     0.811
less_90d_tr1.lag                  0.931      1.074     0.754     1.150
log_dias_treat_imp_sin_na.lag     0.907      1.103     0.825     0.997
comp_bpsc_y3_severe.lag           1.105      0.905     0.869     1.406
policonsumo2.lag                  0.801      1.248     0.649     0.988
edad_al_ing_1                     1.045      0.957     1.026     1.064
ano_nac_corr                      1.052      0.951     1.033     1.071
susinidum_oh                      1.202      0.832     0.802     1.800
susinidum_coc                     1.442      0.694     0.931     2.233
susinidum_pbc                     1.309      0.764     0.855     2.004
susinidum_mar                     1.324      0.755     0.881     1.989
psycom_dum_with                   0.949      1.054     0.873     1.031
psycom_dum_study                  1.726      0.579     1.510     1.972
freq_cons_dum_5day                1.076      0.929     0.985     1.175
cond_oc_dum_2inact                1.103      0.906     0.977     1.246
cond_oc_dum_3unemp                1.050      0.952     0.957     1.153
susprindum_oh                     1.591      0.628     1.050     2.411
susprindum_coc                    1.734      0.577     1.140     2.636
susprindum_pbc                    1.617      0.619     1.067     2.449
susprindum_mar                    1.526      0.655     0.996     2.340

Concordance= 0.602  (se = 0.006 )
Likelihood ratio test= 253  on 20 df,   p=<2e-16
Wald test            = 242  on 20 df,   p=<2e-16
Score (logrank) test = 238  on 20 df,   p=<2e-16,   Robust = 223  p=<2e-16

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.153   0.362   0.456   0.468   0.526   1.746 
[1] "Modality: GP residential\nTime interval: [0,10]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 1378, number of events= 1378 
   (2346 observations deleted due to missingness)

                                  coef exp(coef) se(coef) robust se     z
tr_outcome.lag                -0.02212   0.97812  0.18230   0.14719 -0.15
less_90d_tr1.lag              -0.12076   0.88624  0.14750   0.10850 -1.11
log_dias_treat_imp_sin_na.lag -0.08004   0.92308  0.04426   0.03298 -2.43
comp_bpsc_y3_severe.lag        0.00436   1.00437  0.13899   0.10793  0.04
policonsumo2.lag              -0.22270   0.80035  0.15110   0.12180 -1.83
edad_al_ing_1                  0.01512   1.01523  0.01204   0.01148  1.32
ano_nac_corr                   0.02049   1.02070  0.01183   0.01162  1.76
susinidum_oh                   0.36059   1.43418  0.21840   0.19332  1.87
susinidum_coc                  0.34462   1.41145  0.25671   0.23968  1.44
susinidum_pbc                  0.33183   1.39352  0.23049   0.20635  1.61
susinidum_mar                  0.40104   1.49338  0.21946   0.19405  2.07
psycom_dum_with               -0.07960   0.92349  0.06169   0.06166 -1.29
psycom_dum_study               0.24325   1.27539  0.10323   0.09260  2.63
freq_cons_dum_5day             0.15988   1.17337  0.06363   0.06289  2.54
cond_oc_dum_2inact             0.01093   1.01099  0.10212   0.10164  0.11
cond_oc_dum_3unemp            -0.01225   0.98782  0.08671   0.08836 -0.14
susprindum_oh                  0.23921   1.27025  0.29055   0.17108  1.40
susprindum_coc                 0.16471   1.17905  0.29258   0.17489  0.94
susprindum_pbc                 0.30173   1.35220  0.28413   0.16397  1.84
susprindum_mar                 0.20262   1.22461  0.33503   0.22717  0.89
                              Pr(>|z|)   
tr_outcome.lag                  0.8805   
less_90d_tr1.lag                0.2657   
log_dias_treat_imp_sin_na.lag   0.0152 * 
comp_bpsc_y3_severe.lag         0.9677   
policonsumo2.lag                0.0675 . 
edad_al_ing_1                   0.1880   
ano_nac_corr                    0.0779 . 
susinidum_oh                    0.0621 . 
susinidum_coc                   0.1505   
susinidum_pbc                   0.1078   
susinidum_mar                   0.0388 * 
psycom_dum_with                 0.1967   
psycom_dum_study                0.0086 **
freq_cons_dum_5day              0.0110 * 
cond_oc_dum_2inact              0.9144   
cond_oc_dum_3unemp              0.8897   
susprindum_oh                   0.1620   
susprindum_coc                  0.3463   
susprindum_pbc                  0.0657 . 
susprindum_mar                  0.3724   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    0.978      1.022     0.733     1.305
less_90d_tr1.lag                  0.886      1.128     0.716     1.096
log_dias_treat_imp_sin_na.lag     0.923      1.083     0.865     0.985
comp_bpsc_y3_severe.lag           1.004      0.996     0.813     1.241
policonsumo2.lag                  0.800      1.249     0.630     1.016
edad_al_ing_1                     1.015      0.985     0.993     1.038
ano_nac_corr                      1.021      0.980     0.998     1.044
susinidum_oh                      1.434      0.697     0.982     2.095
susinidum_coc                     1.411      0.708     0.882     2.258
susinidum_pbc                     1.394      0.718     0.930     2.088
susinidum_mar                     1.493      0.670     1.021     2.184
psycom_dum_with                   0.923      1.083     0.818     1.042
psycom_dum_study                  1.275      0.784     1.064     1.529
freq_cons_dum_5day                1.173      0.852     1.037     1.327
cond_oc_dum_2inact                1.011      0.989     0.828     1.234
cond_oc_dum_3unemp                0.988      1.012     0.831     1.175
susprindum_oh                     1.270      0.787     0.908     1.776
susprindum_coc                    1.179      0.848     0.837     1.661
susprindum_pbc                    1.352      0.740     0.981     1.865
susprindum_mar                    1.225      0.817     0.785     1.911

Concordance= 0.579  (se = 0.009 )
Likelihood ratio test= 56.6  on 20 df,   p=2e-05
Wald test            = 77.2  on 20 df,   p=1e-08
Score (logrank) test = 54.3  on 20 df,   p=5e-05,   Robust = 66.7  p=6e-07

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.190   0.415   0.467   0.486   0.533   1.217 
[1] "Modality: WO intensive ambulatory\nTime interval: [0,10]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 439, number of events= 439 
   (710 observations deleted due to missingness)

                                 coef exp(coef) se(coef) robust se     z
tr_outcome.lag                -1.3012    0.2722   0.6078    0.2630 -4.95
less_90d_tr1.lag               0.2431    1.2752   0.3248    0.2038  1.19
log_dias_treat_imp_sin_na.lag  0.0482    1.0494   0.1964    0.0726  0.66
comp_bpsc_y3_severe.lag       -0.1749    0.8395   0.2902    0.2065 -0.85
policonsumo2.lag               0.4071    1.5024   0.3289    0.2066  1.97
edad_al_ing_1                  0.0314    1.0319   0.0239    0.0235  1.34
ano_nac_corr                   0.0300    1.0304   0.0237    0.0237  1.26
susinidum_oh                  -0.0748    0.9280   0.3364    0.4122 -0.18
susinidum_coc                 -0.1454    0.8647   0.3979    0.4532 -0.32
susinidum_pbc                 -0.3397    0.7120   0.3672    0.4399 -0.77
susinidum_mar                 -0.1898    0.8272   0.3454    0.4156 -0.46
psycom_dum_with                0.2123    1.2365   0.1092    0.1048  2.03
psycom_dum_study               0.7697    2.1591   0.1897    0.1878  4.10
freq_cons_dum_5day             0.0433    1.0442   0.1019    0.1040  0.42
cond_oc_dum_2inact             0.1268    1.1352   0.1298    0.1225  1.03
cond_oc_dum_3unemp             0.1225    1.1304   0.1384    0.1315  0.93
susprindum_oh                  0.3221    1.3800   0.3895    0.2781  1.16
susprindum_coc                 0.6083    1.8374   0.3961    0.2869  2.12
susprindum_pbc                 0.6333    1.8838   0.3803    0.2656  2.38
susprindum_mar                 1.1292    3.0932   0.4219    0.2995  3.77
                              Pr(>|z|)    
tr_outcome.lag                 7.5e-07 ***
less_90d_tr1.lag               0.23300    
log_dias_treat_imp_sin_na.lag  0.50647    
comp_bpsc_y3_severe.lag        0.39697    
policonsumo2.lag               0.04880 *  
edad_al_ing_1                  0.18121    
ano_nac_corr                   0.20670    
susinidum_oh                   0.85608    
susinidum_coc                  0.74838    
susinidum_pbc                  0.44007    
susinidum_mar                  0.64795    
psycom_dum_with                0.04280 *  
psycom_dum_study               4.2e-05 ***
freq_cons_dum_5day             0.67737    
cond_oc_dum_2inact             0.30087    
cond_oc_dum_3unemp             0.35156    
susprindum_oh                  0.24687    
susprindum_coc                 0.03399 *  
susprindum_pbc                 0.01712 *  
susprindum_mar                 0.00016 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    0.272      3.674     0.163     0.456
less_90d_tr1.lag                  1.275      0.784     0.855     1.901
log_dias_treat_imp_sin_na.lag     1.049      0.953     0.910     1.210
comp_bpsc_y3_severe.lag           0.840      1.191     0.560     1.258
policonsumo2.lag                  1.502      0.666     1.002     2.252
edad_al_ing_1                     1.032      0.969     0.985     1.080
ano_nac_corr                      1.030      0.970     0.984     1.080
susinidum_oh                      0.928      1.078     0.414     2.082
susinidum_coc                     0.865      1.156     0.356     2.102
susinidum_pbc                     0.712      1.404     0.301     1.686
susinidum_mar                     0.827      1.209     0.366     1.868
psycom_dum_with                   1.237      0.809     1.007     1.518
psycom_dum_study                  2.159      0.463     1.494     3.120
freq_cons_dum_5day                1.044      0.958     0.852     1.280
cond_oc_dum_2inact                1.135      0.881     0.893     1.443
cond_oc_dum_3unemp                1.130      0.885     0.873     1.463
susprindum_oh                     1.380      0.725     0.800     2.380
susprindum_coc                    1.837      0.544     1.047     3.224
susprindum_pbc                    1.884      0.531     1.119     3.171
susprindum_mar                    3.093      0.323     1.720     5.563

Concordance= 0.615  (se = 0.015 )
Likelihood ratio test= 46.8  on 20 df,   p=6e-04
Wald test            = 66.7  on 20 df,   p=6e-07
Score (logrank) test = 44.6  on 20 df,   p=0.001,   Robust = 53.9  p=6e-05

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.138   0.362   0.482   0.507   0.590   1.829 
[1] "Modality: WO residential\nTime interval: (10,20]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 510, number of events= 510 
   (460 observations deleted due to missingness)

                                  coef exp(coef) se(coef) robust se     z
tr_outcome.lag                 0.02037   1.02058  0.32347   0.28663  0.07
less_90d_tr1.lag              -0.05381   0.94761  0.47776   0.33189 -0.16
log_dias_treat_imp_sin_na.lag -0.04359   0.95734  0.13190   0.08040 -0.54
comp_bpsc_y3_severe.lag       -0.22175   0.80112  0.36638   0.27653 -0.80
policonsumo2.lag              -0.27505   0.75953  0.41429   0.24540 -1.12
edad_al_ing_1                 -0.01107   0.98899  0.01968   0.01844 -0.60
ano_nac_corr                  -0.01499   0.98512  0.01893   0.01757 -0.85
susinidum_oh                  -0.28712   0.75043  0.29717   0.42526 -0.68
susinidum_coc                 -0.22276   0.80031  0.34277   0.45219 -0.49
susinidum_pbc                 -0.49527   0.60940  0.30801   0.43812 -1.13
susinidum_mar                 -0.29516   0.74441  0.29779   0.42509 -0.69
psycom_dum_with                0.11958   1.12702  0.14013   0.13615  0.88
psycom_dum_study               0.08277   1.08629  0.16386   0.16597  0.50
freq_cons_dum_5day             0.04525   1.04629  0.10570   0.10638  0.43
cond_oc_dum_2inact             0.09185   1.09620  0.16522   0.14958  0.61
cond_oc_dum_3unemp            -0.05006   0.95117  0.16480   0.15168 -0.33
susprindum_oh                  0.13345   1.14276  0.29473   0.29308  0.46
susprindum_coc                -0.33084   0.71832  0.30710   0.31920 -1.04
susprindum_pbc                 0.00516   1.00517  0.28530   0.29053  0.02
susprindum_mar                -0.88875   0.41117  0.53601   0.39663 -2.24
                              Pr(>|z|)  
tr_outcome.lag                   0.943  
less_90d_tr1.lag                 0.871  
log_dias_treat_imp_sin_na.lag    0.588  
comp_bpsc_y3_severe.lag          0.423  
policonsumo2.lag                 0.262  
edad_al_ing_1                    0.548  
ano_nac_corr                     0.394  
susinidum_oh                     0.500  
susinidum_coc                    0.622  
susinidum_pbc                    0.258  
susinidum_mar                    0.487  
psycom_dum_with                  0.380  
psycom_dum_study                 0.618  
freq_cons_dum_5day               0.671  
cond_oc_dum_2inact               0.539  
cond_oc_dum_3unemp               0.741  
susprindum_oh                    0.649  
susprindum_coc                   0.300  
susprindum_pbc                   0.986  
susprindum_mar                   0.025 *
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    1.021      0.980     0.582     1.790
less_90d_tr1.lag                  0.948      1.055     0.494     1.816
log_dias_treat_imp_sin_na.lag     0.957      1.045     0.818     1.121
comp_bpsc_y3_severe.lag           0.801      1.248     0.466     1.377
policonsumo2.lag                  0.760      1.317     0.470     1.229
edad_al_ing_1                     0.989      1.011     0.954     1.025
ano_nac_corr                      0.985      1.015     0.952     1.020
susinidum_oh                      0.750      1.333     0.326     1.727
susinidum_coc                     0.800      1.250     0.330     1.942
susinidum_pbc                     0.609      1.641     0.258     1.438
susinidum_mar                     0.744      1.343     0.324     1.713
psycom_dum_with                   1.127      0.887     0.863     1.472
psycom_dum_study                  1.086      0.921     0.785     1.504
freq_cons_dum_5day                1.046      0.956     0.849     1.289
cond_oc_dum_2inact                1.096      0.912     0.818     1.470
cond_oc_dum_3unemp                0.951      1.051     0.707     1.280
susprindum_oh                     1.143      0.875     0.643     2.030
susprindum_coc                    0.718      1.392     0.384     1.343
susprindum_pbc                    1.005      0.995     0.569     1.776
susprindum_mar                    0.411      2.432     0.189     0.895

Concordance= 0.564  (se = 0.014 )
Likelihood ratio test= 32.3  on 20 df,   p=0.04
Wald test            = 46.8  on 20 df,   p=6e-04
Score (logrank) test = 30.6  on 20 df,   p=0.06,   Robust = 38.2  p=0.008

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.72    1.05    1.21    1.30    1.44    3.24 
[1] "Modality: GP intensive ambulatory\nTime interval: (10,20]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 2228, number of events= 2228 
   (2101 observations deleted due to missingness)

                                  coef exp(coef) se(coef) robust se     z
tr_outcome.lag                -0.22051   0.80211  0.20196   0.15221 -1.45
less_90d_tr1.lag               0.15824   1.17144  0.29932   0.20027  0.79
log_dias_treat_imp_sin_na.lag -0.17581   0.83878  0.06732   0.04711 -3.73
comp_bpsc_y3_severe.lag        0.04739   1.04853  0.18277   0.13092  0.36
policonsumo2.lag               0.02008   1.02029  0.22228   0.14928  0.13
edad_al_ing_1                 -0.00154   0.99847  0.00887   0.00882 -0.17
ano_nac_corr                  -0.00251   0.99749  0.00859   0.00858 -0.29
susinidum_oh                   0.05098   1.05230  0.15531   0.17300  0.29
susinidum_coc                  0.15420   1.16673  0.18054   0.19068  0.81
susinidum_pbc                  0.06130   1.06322  0.17251   0.19134  0.32
susinidum_mar                  0.03883   1.03959  0.15757   0.17637  0.22
psycom_dum_with               -0.01992   0.98028  0.04721   0.04696 -0.42
psycom_dum_study              -0.05966   0.94209  0.06569   0.06467 -0.92
freq_cons_dum_5day            -0.01222   0.98786  0.04390   0.04387 -0.28
cond_oc_dum_2inact             0.06559   1.06779  0.06486   0.06500  1.01
cond_oc_dum_3unemp             0.07510   1.07800  0.04738   0.04737  1.59
susprindum_oh                  0.08632   1.09016  0.17649   0.18533  0.47
susprindum_coc                 0.04682   1.04793  0.17742   0.18631  0.25
susprindum_pbc                 0.07389   1.07668  0.17324   0.18203  0.41
susprindum_mar                 0.13331   1.14260  0.19634   0.20113  0.66
                              Pr(>|z|)    
tr_outcome.lag                 0.14742    
less_90d_tr1.lag               0.42946    
log_dias_treat_imp_sin_na.lag  0.00019 ***
comp_bpsc_y3_severe.lag        0.71735    
policonsumo2.lag               0.89298    
edad_al_ing_1                  0.86174    
ano_nac_corr                   0.76954    
susinidum_oh                   0.76823    
susinidum_coc                  0.41870    
susinidum_pbc                  0.74870    
susinidum_mar                  0.82576    
psycom_dum_with                0.67141    
psycom_dum_study               0.35627    
freq_cons_dum_5day             0.78062    
cond_oc_dum_2inact             0.31295    
cond_oc_dum_3unemp             0.11288    
susprindum_oh                  0.64136    
susprindum_coc                 0.80159    
susprindum_pbc                 0.68481    
susprindum_mar                 0.50745    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    0.802      1.247     0.595      1.08
less_90d_tr1.lag                  1.171      0.854     0.791      1.73
log_dias_treat_imp_sin_na.lag     0.839      1.192     0.765      0.92
comp_bpsc_y3_severe.lag           1.049      0.954     0.811      1.36
policonsumo2.lag                  1.020      0.980     0.761      1.37
edad_al_ing_1                     0.998      1.002     0.981      1.02
ano_nac_corr                      0.997      1.003     0.981      1.01
susinidum_oh                      1.052      0.950     0.750      1.48
susinidum_coc                     1.167      0.857     0.803      1.70
susinidum_pbc                     1.063      0.941     0.731      1.55
susinidum_mar                     1.040      0.962     0.736      1.47
psycom_dum_with                   0.980      1.020     0.894      1.07
psycom_dum_study                  0.942      1.061     0.830      1.07
freq_cons_dum_5day                0.988      1.012     0.906      1.08
cond_oc_dum_2inact                1.068      0.937     0.940      1.21
cond_oc_dum_3unemp                1.078      0.928     0.982      1.18
susprindum_oh                     1.090      0.917     0.758      1.57
susprindum_coc                    1.048      0.954     0.727      1.51
susprindum_pbc                    1.077      0.929     0.754      1.54
susprindum_mar                    1.143      0.875     0.770      1.69

Concordance= 0.529  (se = 0.007 )
Likelihood ratio test= 68.9  on 20 df,   p=3e-07
Wald test            = 96.2  on 20 df,   p=6e-12
Score (logrank) test = 59.1  on 20 df,   p=1e-05,   Robust = 49.1  p=3e-04

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.589   0.812   0.847   0.886   0.877   2.101 
[1] "Modality: basic ambulatory\nTime interval: (10,20]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 1787, number of events= 1787 
   (1705 observations deleted due to missingness)

                                  coef exp(coef) se(coef) robust se     z
tr_outcome.lag                -0.12932   0.87869  0.25813   0.18571 -0.70
less_90d_tr1.lag              -0.15985   0.85227  0.42237   0.34156 -0.47
log_dias_treat_imp_sin_na.lag -0.12757   0.88023  0.08020   0.06162 -2.07
comp_bpsc_y3_severe.lag       -0.08762   0.91610  0.28847   0.22146 -0.40
policonsumo2.lag               0.21017   1.23388  0.24199   0.20255  1.04
edad_al_ing_1                 -0.00577   0.99424  0.01003   0.01001 -0.58
ano_nac_corr                  -0.00986   0.99019  0.00973   0.00982 -1.00
susinidum_oh                  -0.10656   0.89892  0.17572   0.19057 -0.56
susinidum_coc                 -0.25140   0.77771  0.20950   0.21642 -1.16
susinidum_pbc                 -0.02585   0.97448  0.19592   0.21091 -0.12
susinidum_mar                 -0.07484   0.92789  0.17887   0.19301 -0.39
psycom_dum_with               -0.05706   0.94453  0.05251   0.05189 -1.10
psycom_dum_study              -0.15100   0.85984  0.07230   0.07389 -2.04
freq_cons_dum_5day             0.02770   1.02809  0.05517   0.05588  0.50
cond_oc_dum_2inact            -0.02770   0.97268  0.07139   0.07290 -0.38
cond_oc_dum_3unemp            -0.01719   0.98295  0.05936   0.05787 -0.30
susprindum_oh                  0.04424   1.04523  0.20165   0.17037  0.26
susprindum_coc                 0.06049   1.06235  0.20551   0.17376  0.35
susprindum_pbc                -0.01698   0.98316  0.20146   0.17168 -0.10
susprindum_mar                 0.21749   1.24296  0.21387   0.18526  1.17
                              Pr(>|z|)  
tr_outcome.lag                   0.486  
less_90d_tr1.lag                 0.640  
log_dias_treat_imp_sin_na.lag    0.038 *
comp_bpsc_y3_severe.lag          0.692  
policonsumo2.lag                 0.299  
edad_al_ing_1                    0.564  
ano_nac_corr                     0.316  
susinidum_oh                     0.576  
susinidum_coc                    0.245  
susinidum_pbc                    0.902  
susinidum_mar                    0.698  
psycom_dum_with                  0.271  
psycom_dum_study                 0.041 *
freq_cons_dum_5day               0.620  
cond_oc_dum_2inact               0.704  
cond_oc_dum_3unemp               0.766  
susprindum_oh                    0.795  
susprindum_coc                   0.728  
susprindum_pbc                   0.921  
susprindum_mar                   0.240  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    0.879      1.138     0.611     1.264
less_90d_tr1.lag                  0.852      1.173     0.436     1.665
log_dias_treat_imp_sin_na.lag     0.880      1.136     0.780     0.993
comp_bpsc_y3_severe.lag           0.916      1.092     0.594     1.414
policonsumo2.lag                  1.234      0.810     0.830     1.835
edad_al_ing_1                     0.994      1.006     0.975     1.014
ano_nac_corr                      0.990      1.010     0.971     1.009
susinidum_oh                      0.899      1.112     0.619     1.306
susinidum_coc                     0.778      1.286     0.509     1.189
susinidum_pbc                     0.974      1.026     0.645     1.473
susinidum_mar                     0.928      1.078     0.636     1.355
psycom_dum_with                   0.945      1.059     0.853     1.046
psycom_dum_study                  0.860      1.163     0.744     0.994
freq_cons_dum_5day                1.028      0.973     0.921     1.147
cond_oc_dum_2inact                0.973      1.028     0.843     1.122
cond_oc_dum_3unemp                0.983      1.017     0.878     1.101
susprindum_oh                     1.045      0.957     0.749     1.460
susprindum_coc                    1.062      0.941     0.756     1.493
susprindum_pbc                    0.983      1.017     0.702     1.376
susprindum_mar                    1.243      0.805     0.864     1.787

Concordance= 0.535  (se = 0.008 )
Likelihood ratio test= 30.9  on 20 df,   p=0.06
Wald test            = 36.3  on 20 df,   p=0.01
Score (logrank) test = 29.6  on 20 df,   p=0.08,   Robust = 30.6  p=0.06

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.746   1.027   1.092   1.115   1.173   2.260 
[1] "Modality: GP residential\nTime interval: (10,20]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 1104, number of events= 1104 
   (1012 observations deleted due to missingness)

                                  coef exp(coef) se(coef) robust se     z
tr_outcome.lag                 0.16084   1.17450  0.22786   0.16977  0.95
less_90d_tr1.lag              -0.49178   0.61154  0.34685   0.23196 -2.12
log_dias_treat_imp_sin_na.lag -0.34202   0.71034  0.10550   0.06216 -5.50
comp_bpsc_y3_severe.lag        0.30136   1.35170  0.25249   0.19164  1.57
policonsumo2.lag               0.30428   1.35564  0.34606   0.18312  1.66
edad_al_ing_1                  0.00602   1.00604  0.01385   0.01406  0.43
ano_nac_corr                   0.00667   1.00669  0.01397   0.01379  0.48
susinidum_oh                  -0.15110   0.85976  0.21234   0.18102 -0.83
susinidum_coc                 -0.03124   0.96925  0.26184   0.22544 -0.14
susinidum_pbc                 -0.28296   0.75355  0.22751   0.19856 -1.43
susinidum_mar                 -0.13798   0.87111  0.21120   0.17930 -0.77
psycom_dum_with               -0.07512   0.92763  0.07035   0.06946 -1.08
psycom_dum_study              -0.19636   0.82172  0.09873   0.09277 -2.12
freq_cons_dum_5day            -0.02582   0.97451  0.06870   0.06894 -0.37
cond_oc_dum_2inact            -0.00504   0.99497  0.11271   0.11272 -0.04
cond_oc_dum_3unemp             0.11897   1.12634  0.09198   0.08874  1.34
susprindum_oh                 -0.03578   0.96485  0.31288   0.27870 -0.13
susprindum_coc                -0.16252   0.85000  0.31542   0.28194 -0.58
susprindum_pbc                -0.15322   0.85794  0.30652   0.27433 -0.56
susprindum_mar                 0.41912   1.52062  0.35401   0.32844  1.28
                              Pr(>|z|)    
tr_outcome.lag                   0.343    
less_90d_tr1.lag                 0.034 *  
log_dias_treat_imp_sin_na.lag  3.8e-08 ***
comp_bpsc_y3_severe.lag          0.116    
policonsumo2.lag                 0.097 .  
edad_al_ing_1                    0.669    
ano_nac_corr                     0.629    
susinidum_oh                     0.404    
susinidum_coc                    0.890    
susinidum_pbc                    0.154    
susinidum_mar                    0.442    
psycom_dum_with                  0.279    
psycom_dum_study                 0.034 *  
freq_cons_dum_5day               0.708    
cond_oc_dum_2inact               0.964    
cond_oc_dum_3unemp               0.180    
susprindum_oh                    0.898    
susprindum_coc                   0.564    
susprindum_pbc                   0.576    
susprindum_mar                   0.202    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    1.175      0.851     0.842     1.638
less_90d_tr1.lag                  0.612      1.635     0.388     0.964
log_dias_treat_imp_sin_na.lag     0.710      1.408     0.629     0.802
comp_bpsc_y3_severe.lag           1.352      0.740     0.928     1.968
policonsumo2.lag                  1.356      0.738     0.947     1.941
edad_al_ing_1                     1.006      0.994     0.979     1.034
ano_nac_corr                      1.007      0.993     0.980     1.034
susinidum_oh                      0.860      1.163     0.603     1.226
susinidum_coc                     0.969      1.032     0.623     1.508
susinidum_pbc                     0.754      1.327     0.511     1.112
susinidum_mar                     0.871      1.148     0.613     1.238
psycom_dum_with                   0.928      1.078     0.810     1.063
psycom_dum_study                  0.822      1.217     0.685     0.986
freq_cons_dum_5day                0.975      1.026     0.851     1.115
cond_oc_dum_2inact                0.995      1.005     0.798     1.241
cond_oc_dum_3unemp                1.126      0.888     0.947     1.340
susprindum_oh                     0.965      1.036     0.559     1.666
susprindum_coc                    0.850      1.176     0.489     1.477
susprindum_pbc                    0.858      1.166     0.501     1.469
susprindum_mar                    1.521      0.658     0.799     2.895

Concordance= 0.551  (se = 0.01 )
Likelihood ratio test= 53.5  on 20 df,   p=7e-05
Wald test            = 82.3  on 20 df,   p=2e-09
Score (logrank) test = 50.3  on 20 df,   p=2e-04,   Robust = 51.4  p=1e-04

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.59    1.14    1.22    1.30    1.35    4.01 
[1] "Modality: WO intensive ambulatory\nTime interval: (10,20]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 383, number of events= 383 
   (363 observations deleted due to missingness)

                                 coef exp(coef) se(coef) robust se     z
tr_outcome.lag                 0.0139    1.0140   0.5205    0.2113  0.07
less_90d_tr1.lag              -0.1059    0.8995   0.6503    0.2745 -0.39
log_dias_treat_imp_sin_na.lag -0.2009    0.8180   0.2031    0.0842 -2.38
comp_bpsc_y3_severe.lag        0.2002    1.2216   0.4707    0.2543  0.79
policonsumo2.lag               0.1453    1.1563   0.5554    0.2622  0.55
edad_al_ing_1                  0.0181    1.0183   0.0229    0.0233  0.78
ano_nac_corr                   0.0110    1.0110   0.0226    0.0218  0.50
susinidum_oh                  -0.1997    0.8190   0.3295    0.2298 -0.87
susinidum_coc                 -0.2145    0.8069   0.3881    0.2883 -0.74
susinidum_pbc                 -0.3852    0.6803   0.3518    0.2681 -1.44
susinidum_mar                 -0.2611    0.7702   0.3303    0.2274 -1.15
psycom_dum_with               -0.0695    0.9329   0.1233    0.1172 -0.59
psycom_dum_study              -0.0870    0.9166   0.1661    0.1586 -0.55
freq_cons_dum_5day             0.0223    1.0226   0.1079    0.1065  0.21
cond_oc_dum_2inact            -0.2520    0.7772   0.1402    0.1450 -1.74
cond_oc_dum_3unemp            -0.1516    0.8594   0.1437    0.1478 -1.03
susprindum_oh                 -0.1029    0.9022   0.4157    0.3344 -0.31
susprindum_coc                 0.1311    1.1400   0.4138    0.3210  0.41
susprindum_pbc                 0.0489    1.0501   0.4034    0.3065  0.16
susprindum_mar                 0.3545    1.4254   0.4304    0.3513  1.01
                              Pr(>|z|)  
tr_outcome.lag                   0.948  
less_90d_tr1.lag                 0.700  
log_dias_treat_imp_sin_na.lag    0.017 *
comp_bpsc_y3_severe.lag          0.431  
policonsumo2.lag                 0.580  
edad_al_ing_1                    0.435  
ano_nac_corr                     0.615  
susinidum_oh                     0.385  
susinidum_coc                    0.457  
susinidum_pbc                    0.151  
susinidum_mar                    0.251  
psycom_dum_with                  0.553  
psycom_dum_study                 0.583  
freq_cons_dum_5day               0.834  
cond_oc_dum_2inact               0.082 .
cond_oc_dum_3unemp               0.305  
susprindum_oh                    0.758  
susprindum_coc                   0.683  
susprindum_pbc                   0.873  
susprindum_mar                   0.313  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    1.014      0.986     0.670     1.534
less_90d_tr1.lag                  0.900      1.112     0.525     1.541
log_dias_treat_imp_sin_na.lag     0.818      1.222     0.694     0.965
comp_bpsc_y3_severe.lag           1.222      0.819     0.742     2.011
policonsumo2.lag                  1.156      0.865     0.692     1.933
edad_al_ing_1                     1.018      0.982     0.973     1.066
ano_nac_corr                      1.011      0.989     0.969     1.055
susinidum_oh                      0.819      1.221     0.522     1.285
susinidum_coc                     0.807      1.239     0.459     1.420
susinidum_pbc                     0.680      1.470     0.402     1.150
susinidum_mar                     0.770      1.298     0.493     1.203
psycom_dum_with                   0.933      1.072     0.741     1.174
psycom_dum_study                  0.917      1.091     0.672     1.251
freq_cons_dum_5day                1.023      0.978     0.830     1.260
cond_oc_dum_2inact                0.777      1.287     0.585     1.033
cond_oc_dum_3unemp                0.859      1.164     0.643     1.148
susprindum_oh                     0.902      1.108     0.468     1.738
susprindum_coc                    1.140      0.877     0.608     2.139
susprindum_pbc                    1.050      0.952     0.576     1.915
susprindum_mar                    1.425      0.702     0.716     2.838

Concordance= 0.56  (se = 0.016 )
Likelihood ratio test= 14.7  on 20 df,   p=0.8
Wald test            = 23.1  on 20 df,   p=0.3
Score (logrank) test = 15  on 20 df,   p=0.8,   Robust = 21.1  p=0.4

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.73    1.28    1.46    1.47    1.65    2.79 
[1] "Modality: WO residential\nTime interval: (20,30]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 337, number of events= 337 
   (312 observations deleted due to missingness)

                                  coef exp(coef) se(coef) robust se     z
tr_outcome.lag                -0.52775   0.58993  0.59758   0.55588 -0.95
less_90d_tr1.lag               1.28742   3.62343  0.63270   0.69793  1.84
log_dias_treat_imp_sin_na.lag  0.37313   1.45227  0.21591   0.17707  2.11
comp_bpsc_y3_severe.lag       -0.44744   0.63926  0.53809   0.42889 -1.04
policonsumo2.lag              -1.35950   0.25679  0.63831   0.50568 -2.69
edad_al_ing_1                  0.00477   1.00478  0.02813   0.02935  0.16
ano_nac_corr                   0.01147   1.01153  0.02742   0.02843  0.40
susinidum_oh                  -0.90099   0.40617  0.36726   0.34401 -2.62
susinidum_coc                 -0.42751   0.65213  0.44421   0.46878 -0.91
susinidum_pbc                 -0.80738   0.44602  0.38152   0.35273 -2.29
susinidum_mar                 -0.67932   0.50696  0.37085   0.34238 -1.98
psycom_dum_with                0.10540   1.11116  0.16856   0.16076  0.66
psycom_dum_study               0.16165   1.17544  0.19683   0.18720  0.86
freq_cons_dum_5day            -0.11891   0.88789  0.13535   0.12817 -0.93
cond_oc_dum_2inact             0.01787   1.01803  0.21311   0.20359  0.09
cond_oc_dum_3unemp             0.11626   1.12328  0.21418   0.20141  0.58
susprindum_oh                  0.60703   1.83497  0.36513   0.33605  1.81
susprindum_coc                 0.19494   1.21524  0.36571   0.34826  0.56
susprindum_pbc                 0.54841   1.73050  0.34360   0.32407  1.69
susprindum_mar                -0.07329   0.92933  0.49572   0.46633 -0.16
                              Pr(>|z|)   
tr_outcome.lag                  0.3424   
less_90d_tr1.lag                0.0651 . 
log_dias_treat_imp_sin_na.lag   0.0351 * 
comp_bpsc_y3_severe.lag         0.2968   
policonsumo2.lag                0.0072 **
edad_al_ing_1                   0.8709   
ano_nac_corr                    0.6868   
susinidum_oh                    0.0088 **
susinidum_coc                   0.3618   
susinidum_pbc                   0.0221 * 
susinidum_mar                   0.0472 * 
psycom_dum_with                 0.5120   
psycom_dum_study                0.3879   
freq_cons_dum_5day              0.3535   
cond_oc_dum_2inact              0.9301   
cond_oc_dum_3unemp              0.5638   
susprindum_oh                   0.0709 . 
susprindum_coc                  0.5756   
susprindum_pbc                  0.0906 . 
susprindum_mar                  0.8751   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    0.590      1.695    0.1984     1.754
less_90d_tr1.lag                  3.623      0.276    0.9227    14.230
log_dias_treat_imp_sin_na.lag     1.452      0.689    1.0264     2.055
comp_bpsc_y3_severe.lag           0.639      1.564    0.2758     1.482
policonsumo2.lag                  0.257      3.894    0.0953     0.692
edad_al_ing_1                     1.005      0.995    0.9486     1.064
ano_nac_corr                      1.012      0.989    0.9567     1.070
susinidum_oh                      0.406      2.462    0.2070     0.797
susinidum_coc                     0.652      1.533    0.2602     1.634
susinidum_pbc                     0.446      2.242    0.2234     0.890
susinidum_mar                     0.507      1.973    0.2591     0.992
psycom_dum_with                   1.111      0.900    0.8109     1.523
psycom_dum_study                  1.175      0.851    0.8144     1.696
freq_cons_dum_5day                0.888      1.126    0.6907     1.141
cond_oc_dum_2inact                1.018      0.982    0.6831     1.517
cond_oc_dum_3unemp                1.123      0.890    0.7569     1.667
susprindum_oh                     1.835      0.545    0.9497     3.545
susprindum_coc                    1.215      0.823    0.6141     2.405
susprindum_pbc                    1.731      0.578    0.9169     3.266
susprindum_mar                    0.929      1.076    0.3726     2.318

Concordance= 0.58  (se = 0.019 )
Likelihood ratio test= 34  on 20 df,   p=0.03
Wald test            = 30.1  on 20 df,   p=0.07
Score (logrank) test = 32.6  on 20 df,   p=0.04,   Robust = 30.6  p=0.06

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.49    1.15    1.33    1.46    1.54    5.65 
[1] "Modality: GP intensive ambulatory\nTime interval: (20,30]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 1468, number of events= 1468 
   (1416 observations deleted due to missingness)

                                  coef exp(coef) se(coef) robust se     z
tr_outcome.lag                -0.77020   0.46292  0.32283   0.21267 -3.62
less_90d_tr1.lag               0.75061   2.11829  0.49073   0.32418  2.32
log_dias_treat_imp_sin_na.lag -0.01781   0.98235  0.12088   0.06905 -0.26
comp_bpsc_y3_severe.lag        0.13237   1.14153  0.27442   0.18921  0.70
policonsumo2.lag              -0.09052   0.91346  0.39153   0.19891 -0.46
edad_al_ing_1                  0.00928   1.00932  0.01199   0.01157  0.80
ano_nac_corr                   0.01265   1.01273  0.01185   0.01153  1.10
susinidum_oh                  -0.17332   0.84087  0.17260   0.14459 -1.20
susinidum_coc                 -0.12314   0.88414  0.20924   0.18679 -0.66
susinidum_pbc                 -0.24649   0.78154  0.19359   0.16674 -1.48
susinidum_mar                 -0.14682   0.86345  0.17517   0.14808 -0.99
psycom_dum_with                0.07150   1.07412  0.05853   0.05778  1.24
psycom_dum_study               0.13922   1.14937  0.07958   0.08264  1.68
freq_cons_dum_5day            -0.02035   0.97985  0.05446   0.05442 -0.37
cond_oc_dum_2inact            -0.16760   0.84569  0.08044   0.07567 -2.21
cond_oc_dum_3unemp            -0.07804   0.92492  0.05854   0.05876 -1.33
susprindum_oh                  0.34743   1.41543  0.22476   0.22378  1.55
susprindum_coc                 0.37297   1.45205  0.22768   0.22798  1.64
susprindum_pbc                 0.36533   1.44099  0.21944   0.21990  1.66
susprindum_mar                 0.54446   1.72368  0.24308   0.24195  2.25
                              Pr(>|z|)    
tr_outcome.lag                 0.00029 ***
less_90d_tr1.lag               0.02059 *  
log_dias_treat_imp_sin_na.lag  0.79649    
comp_bpsc_y3_severe.lag        0.48419    
policonsumo2.lag               0.64906    
edad_al_ing_1                  0.42279    
ano_nac_corr                   0.27261    
susinidum_oh                   0.23063    
susinidum_coc                  0.50974    
susinidum_pbc                  0.13932    
susinidum_mar                  0.32145    
psycom_dum_with                0.21597    
psycom_dum_study               0.09206 .  
freq_cons_dum_5day             0.70838    
cond_oc_dum_2inact             0.02676 *  
cond_oc_dum_3unemp             0.18414    
susprindum_oh                  0.12052    
susprindum_coc                 0.10184    
susprindum_pbc                 0.09665 .  
susprindum_mar                 0.02443 *  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    0.463      2.160     0.305     0.702
less_90d_tr1.lag                  2.118      0.472     1.122     3.999
log_dias_treat_imp_sin_na.lag     0.982      1.018     0.858     1.125
comp_bpsc_y3_severe.lag           1.142      0.876     0.788     1.654
policonsumo2.lag                  0.913      1.095     0.619     1.349
edad_al_ing_1                     1.009      0.991     0.987     1.032
ano_nac_corr                      1.013      0.987     0.990     1.036
susinidum_oh                      0.841      1.189     0.633     1.116
susinidum_coc                     0.884      1.131     0.613     1.275
susinidum_pbc                     0.782      1.280     0.564     1.084
susinidum_mar                     0.863      1.158     0.646     1.154
psycom_dum_with                   1.074      0.931     0.959     1.203
psycom_dum_study                  1.149      0.870     0.978     1.351
freq_cons_dum_5day                0.980      1.021     0.881     1.090
cond_oc_dum_2inact                0.846      1.182     0.729     0.981
cond_oc_dum_3unemp                0.925      1.081     0.824     1.038
susprindum_oh                     1.415      0.706     0.913     2.195
susprindum_coc                    1.452      0.689     0.929     2.270
susprindum_pbc                    1.441      0.694     0.936     2.217
susprindum_mar                    1.724      0.580     1.073     2.770

Concordance= 0.537  (se = 0.009 )
Likelihood ratio test= 45.9  on 20 df,   p=8e-04
Wald test            = 52  on 20 df,   p=1e-04
Score (logrank) test = 41.3  on 20 df,   p=0.003,   Robust = 34.9  p=0.02

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.513   0.767   0.823   0.850   0.890   2.288 
[1] "Modality: basic ambulatory\nTime interval: (20,30]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 1208, number of events= 1208 
   (1172 observations deleted due to missingness)

                                  coef exp(coef) se(coef) robust se     z
tr_outcome.lag                 0.05575   1.05733  0.41651   0.26438  0.21
less_90d_tr1.lag              -0.45329   0.63554  0.43659   0.35253 -1.29
log_dias_treat_imp_sin_na.lag -0.15252   0.85854  0.14076   0.08064 -1.89
comp_bpsc_y3_severe.lag       -0.55325   0.57508  0.40430   0.31080 -1.78
policonsumo2.lag               0.25287   1.28771  0.43978   0.28250  0.90
edad_al_ing_1                  0.04213   1.04303  0.01381   0.01319  3.19
ano_nac_corr                   0.04128   1.04214  0.01367   0.01295  3.19
susinidum_oh                   0.01952   1.01971  0.21963   0.21984  0.09
susinidum_coc                  0.09272   1.09716  0.25589   0.25100  0.37
susinidum_pbc                  0.21430   1.23900  0.24964   0.24453  0.88
susinidum_mar                  0.05777   1.05947  0.22158   0.22321  0.26
psycom_dum_with               -0.05744   0.94418  0.06662   0.06799 -0.84
psycom_dum_study              -0.10343   0.90174  0.07985   0.07481 -1.38
freq_cons_dum_5day             0.07591   1.07887  0.06736   0.06549  1.16
cond_oc_dum_2inact            -0.00385   0.99616  0.09586   0.09454 -0.04
cond_oc_dum_3unemp            -0.02597   0.97437  0.06951   0.07100 -0.37
susprindum_oh                 -0.44138   0.64315  0.28577   0.22911 -1.93
susprindum_coc                -0.44717   0.63943  0.29004   0.23458 -1.91
susprindum_pbc                -0.47769   0.62021  0.28560   0.22746 -2.10
susprindum_mar                -0.44252   0.64242  0.29882   0.25360 -1.74
                              Pr(>|z|)   
tr_outcome.lag                  0.8330   
less_90d_tr1.lag                0.1985   
log_dias_treat_imp_sin_na.lag   0.0586 . 
comp_bpsc_y3_severe.lag         0.0751 . 
policonsumo2.lag                0.3707   
edad_al_ing_1                   0.0014 **
ano_nac_corr                    0.0014 **
susinidum_oh                    0.9292   
susinidum_coc                   0.7118   
susinidum_pbc                   0.3808   
susinidum_mar                   0.7958   
psycom_dum_with                 0.3983   
psycom_dum_study                0.1668   
freq_cons_dum_5day              0.2464   
cond_oc_dum_2inact              0.9675   
cond_oc_dum_3unemp              0.7146   
susprindum_oh                   0.0540 . 
susprindum_coc                  0.0566 . 
susprindum_pbc                  0.0357 * 
susprindum_mar                  0.0810 . 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    1.057      0.946     0.630     1.775
less_90d_tr1.lag                  0.636      1.573     0.318     1.268
log_dias_treat_imp_sin_na.lag     0.859      1.165     0.733     1.006
comp_bpsc_y3_severe.lag           0.575      1.739     0.313     1.057
policonsumo2.lag                  1.288      0.777     0.740     2.240
edad_al_ing_1                     1.043      0.959     1.016     1.070
ano_nac_corr                      1.042      0.960     1.016     1.069
susinidum_oh                      1.020      0.981     0.663     1.569
susinidum_coc                     1.097      0.911     0.671     1.794
susinidum_pbc                     1.239      0.807     0.767     2.001
susinidum_mar                     1.059      0.944     0.684     1.641
psycom_dum_with                   0.944      1.059     0.826     1.079
psycom_dum_study                  0.902      1.109     0.779     1.044
freq_cons_dum_5day                1.079      0.927     0.949     1.227
cond_oc_dum_2inact                0.996      1.004     0.828     1.199
cond_oc_dum_3unemp                0.974      1.026     0.848     1.120
susprindum_oh                     0.643      1.555     0.410     1.008
susprindum_coc                    0.639      1.564     0.404     1.013
susprindum_pbc                    0.620      1.612     0.397     0.969
susprindum_mar                    0.642      1.557     0.391     1.056

Concordance= 0.553  (se = 0.009 )
Likelihood ratio test= 30.9  on 20 df,   p=0.06
Wald test            = 42.6  on 20 df,   p=0.002
Score (logrank) test = 29.1  on 20 df,   p=0.09,   Robust = 31.7  p=0.05

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.89    1.40    1.54    1.56    1.67    4.05 
[1] "Modality: GP residential\nTime interval: (20,30]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 612, number of events= 612 
   (578 observations deleted due to missingness)

                                 coef exp(coef) se(coef) robust se     z
tr_outcome.lag                -0.1769    0.8378   0.4834    0.2907 -0.61
less_90d_tr1.lag              -0.2906    0.7478   0.5881    0.3684 -0.79
log_dias_treat_imp_sin_na.lag -0.3352    0.7152   0.1787    0.1274 -2.63
comp_bpsc_y3_severe.lag        0.3234    1.3818   0.4523    0.3102  1.04
policonsumo2.lag               0.4513    1.5704   0.5524    0.3767  1.20
edad_al_ing_1                  0.0155    1.0157   0.0203    0.0182  0.85
ano_nac_corr                   0.0204    1.0206   0.0200    0.0183  1.11
susinidum_oh                  -0.4689    0.6257   0.3172    0.2629 -1.78
susinidum_coc                 -0.0629    0.9391   0.3628    0.3023 -0.21
susinidum_pbc                 -0.3090    0.7342   0.3327    0.2733 -1.13
susinidum_mar                 -0.4797    0.6190   0.3174    0.2605 -1.84
psycom_dum_with               -0.1006    0.9043   0.0943    0.0977 -1.03
psycom_dum_study              -0.0192    0.9810   0.1339    0.1244 -0.15
freq_cons_dum_5day             0.1309    1.1398   0.0948    0.0945  1.38
cond_oc_dum_2inact            -0.0699    0.9325   0.1497    0.1480 -0.47
cond_oc_dum_3unemp            -0.0659    0.9363   0.1298    0.1280 -0.51
susprindum_oh                  0.8377    2.3110   0.4654    0.4005  2.09
susprindum_coc                 0.8259    2.2840   0.4701    0.4049  2.04
susprindum_pbc                 0.8141    2.2571   0.4569    0.3882  2.10
susprindum_mar                 0.5328    1.7036   0.5248    0.4425  1.20
                              Pr(>|z|)   
tr_outcome.lag                  0.5428   
less_90d_tr1.lag                0.4302   
log_dias_treat_imp_sin_na.lag   0.0085 **
comp_bpsc_y3_severe.lag         0.2971   
policonsumo2.lag                0.2308   
edad_al_ing_1                   0.3945   
ano_nac_corr                    0.2652   
susinidum_oh                    0.0745 . 
susinidum_coc                   0.8352   
susinidum_pbc                   0.2582   
susinidum_mar                   0.0655 . 
psycom_dum_with                 0.3033   
psycom_dum_study                0.8772   
freq_cons_dum_5day              0.1661   
cond_oc_dum_2inact              0.6369   
cond_oc_dum_3unemp              0.6069   
susprindum_oh                   0.0365 * 
susprindum_coc                  0.0413 * 
susprindum_pbc                  0.0360 * 
susprindum_mar                  0.2286   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    0.838      1.194     0.474     1.481
less_90d_tr1.lag                  0.748      1.337     0.363     1.540
log_dias_treat_imp_sin_na.lag     0.715      1.398     0.557     0.918
comp_bpsc_y3_severe.lag           1.382      0.724     0.752     2.538
policonsumo2.lag                  1.570      0.637     0.751     3.286
edad_al_ing_1                     1.016      0.985     0.980     1.053
ano_nac_corr                      1.021      0.980     0.985     1.058
susinidum_oh                      0.626      1.598     0.374     1.048
susinidum_coc                     0.939      1.065     0.519     1.698
susinidum_pbc                     0.734      1.362     0.430     1.254
susinidum_mar                     0.619      1.616     0.372     1.031
psycom_dum_with                   0.904      1.106     0.747     1.095
psycom_dum_study                  0.981      1.019     0.769     1.252
freq_cons_dum_5day                1.140      0.877     0.947     1.372
cond_oc_dum_2inact                0.933      1.072     0.698     1.246
cond_oc_dum_3unemp                0.936      1.068     0.729     1.203
susprindum_oh                     2.311      0.433     1.054     5.066
susprindum_coc                    2.284      0.438     1.033     5.051
susprindum_pbc                    2.257      0.443     1.055     4.830
susprindum_mar                    1.704      0.587     0.716     4.055

Concordance= 0.549  (se = 0.013 )
Likelihood ratio test= 30.6  on 20 df,   p=0.06
Wald test            = 49.6  on 20 df,   p=3e-04
Score (logrank) test = 28.9  on 20 df,   p=0.09,   Robust = 32.6  p=0.04

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.287   0.618   0.674   0.699   0.734   2.114 
[1] "Modality: WO intensive ambulatory\nTime interval: (20,30]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 247, number of events= 247 
   (235 observations deleted due to missingness)

                                   coef exp(coef)  se(coef) robust se     z
tr_outcome.lag                -7.22e-02  9.30e-01  8.60e-01  4.46e-01 -0.16
less_90d_tr1.lag              -6.96e-01  4.98e-01  9.59e-01  5.93e-01 -1.17
log_dias_treat_imp_sin_na.lag -4.77e-01  6.21e-01  3.69e-01  2.08e-01 -2.29
comp_bpsc_y3_severe.lag        4.74e-01  1.61e+00  8.48e-01  5.41e-01  0.88
policonsumo2.lag               9.90e-01  2.69e+00  8.05e-01  5.92e-01  1.67
edad_al_ing_1                 -1.56e-02  9.85e-01  3.12e-02  2.97e-02 -0.53
ano_nac_corr                  -1.85e-02  9.82e-01  2.94e-02  2.69e-02 -0.69
susinidum_oh                  -5.46e-01  5.79e-01  5.57e-01  4.73e-01 -1.15
susinidum_coc                 -4.26e-01  6.53e-01  6.33e-01  5.47e-01 -0.78
susinidum_pbc                 -7.19e-01  4.87e-01  5.93e-01  5.02e-01 -1.43
susinidum_mar                 -4.38e-01  6.45e-01  5.65e-01  4.76e-01 -0.92
psycom_dum_with                2.34e-02  1.02e+00  1.46e-01  1.40e-01  0.17
psycom_dum_study               2.92e-01  1.34e+00  2.35e-01  2.58e-01  1.13
freq_cons_dum_5day            -2.47e-01  7.81e-01  1.47e-01  1.42e-01 -1.74
cond_oc_dum_2inact            -6.83e-02  9.34e-01  1.74e-01  1.56e-01 -0.44
cond_oc_dum_3unemp            -2.42e-02  9.76e-01  1.84e-01  1.81e-01 -0.13
susprindum_oh                 -4.26e-01  6.53e-01  4.59e-01  3.61e-01 -1.18
susprindum_coc                 1.33e-01  1.14e+00  4.72e-01  3.97e-01  0.33
susprindum_pbc                 3.51e-05  1.00e+00  4.63e-01  3.79e-01  0.00
susprindum_mar                -1.33e-02  9.87e-01  5.19e-01  4.06e-01 -0.03
                              Pr(>|z|)  
tr_outcome.lag                   0.872  
less_90d_tr1.lag                 0.241  
log_dias_treat_imp_sin_na.lag    0.022 *
comp_bpsc_y3_severe.lag          0.381  
policonsumo2.lag                 0.095 .
edad_al_ing_1                    0.599  
ano_nac_corr                     0.493  
susinidum_oh                     0.248  
susinidum_coc                    0.436  
susinidum_pbc                    0.152  
susinidum_mar                    0.357  
psycom_dum_with                  0.867  
psycom_dum_study                 0.257  
freq_cons_dum_5day               0.082 .
cond_oc_dum_2inact               0.662  
cond_oc_dum_3unemp               0.894  
susprindum_oh                    0.238  
susprindum_coc                   0.739  
susprindum_pbc                   1.000  
susprindum_mar                   0.974  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    0.930      1.075     0.388     2.232
less_90d_tr1.lag                  0.498      2.006     0.156     1.595
log_dias_treat_imp_sin_na.lag     0.621      1.611     0.413     0.933
comp_bpsc_y3_severe.lag           1.607      0.622     0.556     4.640
policonsumo2.lag                  2.691      0.372     0.843     8.589
edad_al_ing_1                     0.985      1.016     0.929     1.044
ano_nac_corr                      0.982      1.019     0.931     1.035
susinidum_oh                      0.579      1.727     0.229     1.464
susinidum_coc                     0.653      1.532     0.223     1.907
susinidum_pbc                     0.487      2.052     0.182     1.303
susinidum_mar                     0.645      1.550     0.254     1.639
psycom_dum_with                   1.024      0.977     0.778     1.347
psycom_dum_study                  1.339      0.747     0.808     2.219
freq_cons_dum_5day                0.781      1.281     0.591     1.032
cond_oc_dum_2inact                0.934      1.071     0.687     1.269
cond_oc_dum_3unemp                0.976      1.024     0.684     1.392
susprindum_oh                     0.653      1.531     0.322     1.325
susprindum_coc                    1.142      0.876     0.524     2.487
susprindum_pbc                    1.000      1.000     0.476     2.102
susprindum_mar                    0.987      1.013     0.445     2.187

Concordance= 0.587  (se = 0.021 )
Likelihood ratio test= 17.1  on 20 df,   p=0.6
Wald test            = 23.7  on 20 df,   p=0.3
Score (logrank) test = 16.7  on 20 df,   p=0.7,   Robust = 20.1  p=0.4

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.90    1.48    1.79    1.92    2.26    5.41 
[1] "Modality: WO residential\nTime interval: (30,50]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 374, number of events= 374 
   (332 observations deleted due to missingness)

                                   coef exp(coef)  se(coef) robust se     z
tr_outcome.lag                -0.266240  0.766255  0.333140  0.156403 -1.70
less_90d_tr1.lag              -0.000868  0.999132  0.362284  0.230311  0.00
log_dias_treat_imp_sin_na.lag -0.074266  0.928425  0.072077  0.022583 -3.29
comp_bpsc_y3_severe.lag        0.172606  1.188397  0.384694  0.156994  1.10
policonsumo2.lag              -0.110303  0.895562  0.413318  0.144697 -0.76
edad_al_ing_1                  0.070663  1.073220  0.031269  0.032755  2.16
ano_nac_corr                   0.070856  1.073426  0.030683  0.032237  2.20
susinidum_oh                   1.203067  3.330315  0.559960  0.309989  3.88
susinidum_coc                  1.489861  4.436480  0.605530  0.345834  4.31
susinidum_pbc                  1.161622  3.195112  0.568769  0.322981  3.60
susinidum_mar                  1.193610  3.298968  0.561149  0.308066  3.87
psycom_dum_with               -0.067138  0.935066  0.150896  0.147822 -0.45
psycom_dum_study               0.000160  1.000160  0.172690  0.165963  0.00
freq_cons_dum_5day            -0.025945  0.974389  0.124841  0.121203 -0.21
cond_oc_dum_2inact            -0.166109  0.846954  0.226493  0.176276 -0.94
cond_oc_dum_3unemp            -0.253565  0.776029  0.230443  0.191113 -1.33
susprindum_oh                 -0.862997  0.421896  0.396358  0.251589 -3.43
susprindum_coc                -0.980906  0.374971  0.401764  0.255461 -3.84
susprindum_pbc                -0.844257  0.429877  0.377333  0.219059 -3.85
susprindum_mar                -0.321914  0.724761  0.572188  0.309357 -1.04
                              Pr(>|z|)    
tr_outcome.lag                 0.08871 .  
less_90d_tr1.lag               0.99699    
log_dias_treat_imp_sin_na.lag  0.00101 ** 
comp_bpsc_y3_severe.lag        0.27158    
policonsumo2.lag               0.44588    
edad_al_ing_1                  0.03098 *  
ano_nac_corr                   0.02795 *  
susinidum_oh                   0.00010 ***
susinidum_coc                  1.6e-05 ***
susinidum_pbc                  0.00032 ***
susinidum_mar                  0.00011 ***
psycom_dum_with                0.64970    
psycom_dum_study               0.99923    
freq_cons_dum_5day             0.83050    
cond_oc_dum_2inact             0.34603    
cond_oc_dum_3unemp             0.18458    
susprindum_oh                  0.00060 ***
susprindum_coc                 0.00012 ***
susprindum_pbc                 0.00012 ***
susprindum_mar                 0.29807    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    0.766      1.305     0.564     1.041
less_90d_tr1.lag                  0.999      1.001     0.636     1.569
log_dias_treat_imp_sin_na.lag     0.928      1.077     0.888     0.970
comp_bpsc_y3_severe.lag           1.188      0.841     0.874     1.617
policonsumo2.lag                  0.896      1.117     0.674     1.189
edad_al_ing_1                     1.073      0.932     1.006     1.144
ano_nac_corr                      1.073      0.932     1.008     1.143
susinidum_oh                      3.330      0.300     1.814     6.114
susinidum_coc                     4.436      0.225     2.252     8.738
susinidum_pbc                     3.195      0.313     1.697     6.017
susinidum_mar                     3.299      0.303     1.804     6.034
psycom_dum_with                   0.935      1.069     0.700     1.249
psycom_dum_study                  1.000      1.000     0.722     1.385
freq_cons_dum_5day                0.974      1.026     0.768     1.236
cond_oc_dum_2inact                0.847      1.181     0.600     1.196
cond_oc_dum_3unemp                0.776      1.289     0.534     1.129
susprindum_oh                     0.422      2.370     0.258     0.691
susprindum_coc                    0.375      2.667     0.227     0.619
susprindum_pbc                    0.430      2.326     0.280     0.660
susprindum_mar                    0.725      1.380     0.395     1.329

Concordance= 0.559  (se = 0.016 )
Likelihood ratio test= 23.1  on 20 df,   p=0.3
Wald test            = 77  on 20 df,   p=1e-08
Score (logrank) test = 22.5  on 20 df,   p=0.3,   Robust = 32.4  p=0.04

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.28    0.80    0.91    0.95    1.07    3.45 
[1] "Modality: GP intensive ambulatory\nTime interval: (30,50]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 1813, number of events= 1813 
   (1667 observations deleted due to missingness)

                                  coef exp(coef) se(coef) robust se     z
tr_outcome.lag                 0.06962   1.07210  0.17575   0.12489  0.56
less_90d_tr1.lag              -0.07466   0.92806  0.20374   0.16249 -0.46
log_dias_treat_imp_sin_na.lag -0.09521   0.90918  0.04869   0.02817 -3.38
comp_bpsc_y3_severe.lag        0.13504   1.14459  0.16871   0.13019  1.04
policonsumo2.lag              -0.12920   0.87880  0.18504   0.12763 -1.01
edad_al_ing_1                  0.04836   1.04955  0.01269   0.01237  3.91
ano_nac_corr                   0.05027   1.05156  0.01267   0.01256  4.00
susinidum_oh                  -0.22012   0.80242  0.18666   0.15267 -1.44
susinidum_coc                 -0.10184   0.90318  0.21646   0.18483 -0.55
susinidum_pbc                 -0.28630   0.75104  0.20610   0.18068 -1.58
susinidum_mar                 -0.26504   0.76718  0.18930   0.15669 -1.69
psycom_dum_with                0.03310   1.03365  0.05340   0.05364  0.62
psycom_dum_study              -0.02673   0.97362  0.07087   0.07139 -0.37
freq_cons_dum_5day             0.02375   1.02404  0.04873   0.04830  0.49
cond_oc_dum_2inact            -0.00351   0.99650  0.07187   0.07491 -0.05
cond_oc_dum_3unemp             0.09120   1.09549  0.05233   0.05132  1.78
susprindum_oh                  0.03331   1.03387  0.21842   0.17404  0.19
susprindum_coc                 0.08333   1.08691  0.22243   0.17940  0.46
susprindum_pbc                 0.06182   1.06377  0.21718   0.17311  0.36
susprindum_mar                 0.06484   1.06699  0.24196   0.19651  0.33
                              Pr(>|z|)    
tr_outcome.lag                 0.57722    
less_90d_tr1.lag               0.64589    
log_dias_treat_imp_sin_na.lag  0.00073 ***
comp_bpsc_y3_severe.lag        0.29961    
policonsumo2.lag               0.31140    
edad_al_ing_1                  9.3e-05 ***
ano_nac_corr                   6.2e-05 ***
susinidum_oh                   0.14937    
susinidum_coc                  0.58165    
susinidum_pbc                  0.11306    
susinidum_mar                  0.09074 .  
psycom_dum_with                0.53724    
psycom_dum_study               0.70810    
freq_cons_dum_5day             0.62291    
cond_oc_dum_2inact             0.96262    
cond_oc_dum_3unemp             0.07552 .  
susprindum_oh                  0.84823    
susprindum_coc                 0.64227    
susprindum_pbc                 0.72103    
susprindum_mar                 0.74142    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    1.072      0.933     0.839     1.369
less_90d_tr1.lag                  0.928      1.078     0.675     1.276
log_dias_treat_imp_sin_na.lag     0.909      1.100     0.860     0.961
comp_bpsc_y3_severe.lag           1.145      0.874     0.887     1.477
policonsumo2.lag                  0.879      1.138     0.684     1.129
edad_al_ing_1                     1.050      0.953     1.024     1.075
ano_nac_corr                      1.052      0.951     1.026     1.078
susinidum_oh                      0.802      1.246     0.595     1.082
susinidum_coc                     0.903      1.107     0.629     1.297
susinidum_pbc                     0.751      1.331     0.527     1.070
susinidum_mar                     0.767      1.303     0.564     1.043
psycom_dum_with                   1.034      0.967     0.930     1.148
psycom_dum_study                  0.974      1.027     0.846     1.120
freq_cons_dum_5day                1.024      0.977     0.932     1.126
cond_oc_dum_2inact                0.996      1.004     0.860     1.154
cond_oc_dum_3unemp                1.095      0.913     0.991     1.211
susprindum_oh                     1.034      0.967     0.735     1.454
susprindum_coc                    1.087      0.920     0.765     1.545
susprindum_pbc                    1.064      0.940     0.758     1.493
susprindum_mar                    1.067      0.937     0.726     1.568

Concordance= 0.541  (se = 0.008 )
Likelihood ratio test= 39.1  on 20 df,   p=0.006
Wald test            = 54.6  on 20 df,   p=5e-05
Score (logrank) test = 38.1  on 20 df,   p=0.009,   Robust = 47.8  p=5e-04

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.54    1.02    1.11    1.14    1.22    2.25 
[1] "Modality: basic ambulatory\nTime interval: (30,50]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 1568, number of events= 1568 
   (1465 observations deleted due to missingness)

                                 coef exp(coef) se(coef) robust se     z
tr_outcome.lag                -0.2806    0.7553   0.2365    0.1623 -1.73
less_90d_tr1.lag              -0.1925    0.8249   0.2784    0.2231 -0.86
log_dias_treat_imp_sin_na.lag -0.0676    0.9347   0.0771    0.0698 -0.97
comp_bpsc_y3_severe.lag        0.0964    1.1012   0.2221    0.1709  0.56
policonsumo2.lag               0.2001    1.2216   0.2863    0.2305  0.87
edad_al_ing_1                  0.0626    1.0646   0.0137    0.0134  4.66
ano_nac_corr                   0.0623    1.0643   0.0134    0.0134  4.63
susinidum_oh                   0.2785    1.3211   0.1894    0.1996  1.40
susinidum_coc                  0.1165    1.1236   0.2247    0.2413  0.48
susinidum_pbc                  0.2363    1.2665   0.2212    0.2252  1.05
susinidum_mar                  0.3165    1.3724   0.1923    0.2039  1.55
psycom_dum_with                0.0814    1.0848   0.0567    0.0577  1.41
psycom_dum_study               0.0763    1.0793   0.0745    0.0721  1.06
freq_cons_dum_5day            -0.0662    0.9359   0.0585    0.0580 -1.14
cond_oc_dum_2inact            -0.0688    0.9335   0.0799    0.0774 -0.89
cond_oc_dum_3unemp             0.0884    1.0924   0.0614    0.0612  1.44
susprindum_oh                 -0.1622    0.8503   0.2267    0.1865 -0.87
susprindum_coc                -0.1144    0.8919   0.2313    0.1933 -0.59
susprindum_pbc                -0.1726    0.8415   0.2259    0.1867 -0.92
susprindum_mar                -0.2593    0.7716   0.2436    0.2006 -1.29
                              Pr(>|z|)    
tr_outcome.lag                   0.084 .  
less_90d_tr1.lag                 0.388    
log_dias_treat_imp_sin_na.lag    0.333    
comp_bpsc_y3_severe.lag          0.573    
policonsumo2.lag                 0.385    
edad_al_ing_1                  3.1e-06 ***
ano_nac_corr                   3.6e-06 ***
susinidum_oh                     0.163    
susinidum_coc                    0.629    
susinidum_pbc                    0.294    
susinidum_mar                    0.121    
psycom_dum_with                  0.159    
psycom_dum_study                 0.290    
freq_cons_dum_5day               0.253    
cond_oc_dum_2inact               0.374    
cond_oc_dum_3unemp               0.149    
susprindum_oh                    0.385    
susprindum_coc                   0.554    
susprindum_pbc                   0.355    
susprindum_mar                   0.196    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    0.755      1.324     0.550      1.04
less_90d_tr1.lag                  0.825      1.212     0.533      1.28
log_dias_treat_imp_sin_na.lag     0.935      1.070     0.815      1.07
comp_bpsc_y3_severe.lag           1.101      0.908     0.788      1.54
policonsumo2.lag                  1.222      0.819     0.777      1.92
edad_al_ing_1                     1.065      0.939     1.037      1.09
ano_nac_corr                      1.064      0.940     1.037      1.09
susinidum_oh                      1.321      0.757     0.893      1.95
susinidum_coc                     1.124      0.890     0.700      1.80
susinidum_pbc                     1.266      0.790     0.815      1.97
susinidum_mar                     1.372      0.729     0.920      2.05
psycom_dum_with                   1.085      0.922     0.969      1.21
psycom_dum_study                  1.079      0.927     0.937      1.24
freq_cons_dum_5day                0.936      1.068     0.835      1.05
cond_oc_dum_2inact                0.933      1.071     0.802      1.09
cond_oc_dum_3unemp                1.092      0.915     0.969      1.23
susprindum_oh                     0.850      1.176     0.590      1.23
susprindum_coc                    0.892      1.121     0.611      1.30
susprindum_pbc                    0.841      1.188     0.584      1.21
susprindum_mar                    0.772      1.296     0.521      1.14

Concordance= 0.552  (se = 0.008 )
Likelihood ratio test= 46.9  on 20 df,   p=6e-04
Wald test            = 53.5  on 20 df,   p=7e-05
Score (logrank) test = 45.8  on 20 df,   p=9e-04,   Robust = 50.2  p=2e-04

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.564   0.755   0.854   0.876   0.966   1.782 
[1] "Modality: GP residential\nTime interval: (30,50]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 756, number of events= 756 
   (698 observations deleted due to missingness)

                                  coef exp(coef) se(coef) robust se     z
tr_outcome.lag                 0.11740   1.12457  0.27673   0.16989  0.69
less_90d_tr1.lag              -0.26249   0.76914  0.35839   0.25620 -1.02
log_dias_treat_imp_sin_na.lag -0.03877   0.96197  0.09439   0.06274 -0.62
comp_bpsc_y3_severe.lag        0.83234   2.29869  0.35360   0.25940  3.21
policonsumo2.lag              -0.60054   0.54852  0.33875   0.21316 -2.82
edad_al_ing_1                  0.08079   1.08415  0.02030   0.01879  4.30
ano_nac_corr                   0.08407   1.08771  0.02014   0.01864  4.51
susinidum_oh                  -0.02922   0.97120  0.24781   0.31946 -0.09
susinidum_coc                  0.38760   1.47344  0.32554   0.36779  1.05
susinidum_pbc                  0.10639   1.11225  0.26543   0.32825  0.32
susinidum_mar                  0.00153   1.00153  0.24791   0.31782  0.00
psycom_dum_with               -0.11299   0.89316  0.08404   0.08392 -1.35
psycom_dum_study              -0.12624   0.88141  0.11726   0.11499 -1.10
freq_cons_dum_5day             0.00821   1.00824  0.08711   0.08190  0.10
cond_oc_dum_2inact             0.15730   1.17035  0.13868   0.14540  1.08
cond_oc_dum_3unemp             0.24127   1.27287  0.11430   0.10752  2.24
susprindum_oh                  0.17025   1.18561  0.33711   0.28315  0.60
susprindum_coc                -0.13689   0.87207  0.34443   0.29566 -0.46
susprindum_pbc                -0.01076   0.98930  0.32630   0.27168 -0.04
susprindum_mar                 0.12651   1.13486  0.37918   0.32173  0.39
                              Pr(>|z|)    
tr_outcome.lag                  0.4895    
less_90d_tr1.lag                0.3056    
log_dias_treat_imp_sin_na.lag   0.5366    
comp_bpsc_y3_severe.lag         0.0013 ** 
policonsumo2.lag                0.0048 ** 
edad_al_ing_1                  1.7e-05 ***
ano_nac_corr                   6.5e-06 ***
susinidum_oh                    0.9271    
susinidum_coc                   0.2919    
susinidum_pbc                   0.7459    
susinidum_mar                   0.9962    
psycom_dum_with                 0.1782    
psycom_dum_study                0.2723    
freq_cons_dum_5day              0.9201    
cond_oc_dum_2inact              0.2793    
cond_oc_dum_3unemp              0.0248 *  
susprindum_oh                   0.5476    
susprindum_coc                  0.6434    
susprindum_pbc                  0.9684    
susprindum_mar                  0.6942    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    1.125      0.889     0.806     1.569
less_90d_tr1.lag                  0.769      1.300     0.466     1.271
log_dias_treat_imp_sin_na.lag     0.962      1.040     0.851     1.088
comp_bpsc_y3_severe.lag           2.299      0.435     1.383     3.822
policonsumo2.lag                  0.549      1.823     0.361     0.833
edad_al_ing_1                     1.084      0.922     1.045     1.125
ano_nac_corr                      1.088      0.919     1.049     1.128
susinidum_oh                      0.971      1.030     0.519     1.817
susinidum_coc                     1.473      0.679     0.717     3.030
susinidum_pbc                     1.112      0.899     0.585     2.116
susinidum_mar                     1.002      0.998     0.537     1.867
psycom_dum_with                   0.893      1.120     0.758     1.053
psycom_dum_study                  0.881      1.135     0.704     1.104
freq_cons_dum_5day                1.008      0.992     0.859     1.184
cond_oc_dum_2inact                1.170      0.854     0.880     1.556
cond_oc_dum_3unemp                1.273      0.786     1.031     1.571
susprindum_oh                     1.186      0.843     0.681     2.065
susprindum_coc                    0.872      1.147     0.489     1.557
susprindum_pbc                    0.989      1.011     0.581     1.685
susprindum_mar                    1.135      0.881     0.604     2.132

Concordance= 0.568  (se = 0.012 )
Likelihood ratio test= 35.3  on 20 df,   p=0.02
Wald test            = 52.5  on 20 df,   p=1e-04
Score (logrank) test = 35.3  on 20 df,   p=0.02,   Robust = 42.6  p=0.002

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.356   0.747   0.864   0.881   0.997   2.334 
[1] "Modality: WO intensive ambulatory\nTime interval: (30,50]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 272, number of events= 272 
   (247 observations deleted due to missingness)

                                 coef exp(coef) se(coef) robust se     z
tr_outcome.lag                -0.8814    0.4142   0.5441    0.5149 -1.71
less_90d_tr1.lag               1.5933    4.9200   0.6802    0.5705  2.79
log_dias_treat_imp_sin_na.lag  0.1368    1.1466   0.1408    0.0886  1.54
comp_bpsc_y3_severe.lag       -0.8961    0.4082   0.5770    0.4669 -1.92
policonsumo2.lag               0.1904    1.2098   0.6121    0.5494  0.35
edad_al_ing_1                  0.0287    1.0291   0.0334    0.0299  0.96
ano_nac_corr                   0.0249    1.0252   0.0327    0.0303  0.82
susinidum_oh                  -0.5643    0.5688   0.4262    0.4298 -1.31
susinidum_coc                 -0.4115    0.6626   0.5134    0.5474 -0.75
susinidum_pbc                 -0.2852    0.7518   0.4498    0.4534 -0.63
susinidum_mar                 -0.3348    0.7155   0.4343    0.4430 -0.76
psycom_dum_with                0.1404    1.1507   0.1441    0.1395  1.01
psycom_dum_study              -0.2148    0.8067   0.2120    0.1910 -1.12
freq_cons_dum_5day             0.0442    1.0452   0.1350    0.1374  0.32
cond_oc_dum_2inact             0.0642    1.0663   0.1672    0.1630  0.39
cond_oc_dum_3unemp             0.1041    1.1097   0.1727    0.1660  0.63
susprindum_oh                 -0.3823    0.6823   0.4451    0.2921 -1.31
susprindum_coc                -0.1114    0.8946   0.4555    0.2828 -0.39
susprindum_pbc                -0.3088    0.7343   0.4311    0.2677 -1.15
susprindum_mar                -0.1062    0.8993   0.4811    0.3316 -0.32
                              Pr(>|z|)   
tr_outcome.lag                  0.0870 . 
less_90d_tr1.lag                0.0052 **
log_dias_treat_imp_sin_na.lag   0.1225   
comp_bpsc_y3_severe.lag         0.0550 . 
policonsumo2.lag                0.7289   
edad_al_ing_1                   0.3377   
ano_nac_corr                    0.4116   
susinidum_oh                    0.1893   
susinidum_coc                   0.4522   
susinidum_pbc                   0.5293   
susinidum_mar                   0.4498   
psycom_dum_with                 0.3141   
psycom_dum_study                0.2607   
freq_cons_dum_5day              0.7477   
cond_oc_dum_2inact              0.6937   
cond_oc_dum_3unemp              0.5307   
susprindum_oh                   0.1906   
susprindum_coc                  0.6936   
susprindum_pbc                  0.2486   
susprindum_mar                  0.7489   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    0.414      2.414     0.151      1.14
less_90d_tr1.lag                  4.920      0.203     1.608     15.05
log_dias_treat_imp_sin_na.lag     1.147      0.872     0.964      1.36
comp_bpsc_y3_severe.lag           0.408      2.450     0.163      1.02
policonsumo2.lag                  1.210      0.827     0.412      3.55
edad_al_ing_1                     1.029      0.972     0.971      1.09
ano_nac_corr                      1.025      0.975     0.966      1.09
susinidum_oh                      0.569      1.758     0.245      1.32
susinidum_coc                     0.663      1.509     0.227      1.94
susinidum_pbc                     0.752      1.330     0.309      1.83
susinidum_mar                     0.715      1.398     0.300      1.70
psycom_dum_with                   1.151      0.869     0.875      1.51
psycom_dum_study                  0.807      1.240     0.555      1.17
freq_cons_dum_5day                1.045      0.957     0.798      1.37
cond_oc_dum_2inact                1.066      0.938     0.775      1.47
cond_oc_dum_3unemp                1.110      0.901     0.802      1.54
susprindum_oh                     0.682      1.466     0.385      1.21
susprindum_coc                    0.895      1.118     0.514      1.56
susprindum_pbc                    0.734      1.362     0.435      1.24
susprindum_mar                    0.899      1.112     0.469      1.72

Concordance= 0.565  (se = 0.018 )
Likelihood ratio test= 23.4  on 20 df,   p=0.3
Wald test            = 47.7  on 20 df,   p=5e-04
Score (logrank) test = 21.3  on 20 df,   p=0.4,   Robust = 29  p=0.09

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.95    1.52    1.83    1.96    2.19    8.02 
[1] "Modality: WO residential\nTime interval: (50,70]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 179, number of events= 179 
   (161 observations deleted due to missingness)

                                  coef exp(coef) se(coef) robust se     z
tr_outcome.lag                -0.34247   0.71001  0.58467   0.53581 -0.64
less_90d_tr1.lag               0.21249   1.23676  0.75809   0.62317  0.34
log_dias_treat_imp_sin_na.lag -0.54691   0.57873  0.34815   0.24568 -2.23
comp_bpsc_y3_severe.lag        0.61124   1.84271  0.82117   0.69060  0.89
policonsumo2.lag               0.37707   1.45801  0.66872   0.38515  0.98
edad_al_ing_1                  0.03296   1.03351  0.05659   0.05666  0.58
ano_nac_corr                   0.04657   1.04767  0.05529   0.05527  0.84
susinidum_oh                  -0.86691   0.42025  0.63994   0.44744 -1.94
susinidum_coc                 -0.57378   0.56339  0.72954   0.51859 -1.11
susinidum_pbc                 -0.76562   0.46504  0.65980   0.46489 -1.65
susinidum_mar                 -0.41579   0.65982  0.63825   0.47410 -0.88
psycom_dum_with               -0.57579   0.56226  0.22564   0.21500 -2.68
psycom_dum_study              -0.64845   0.52285  0.26350   0.27055 -2.40
freq_cons_dum_5day             0.03824   1.03898  0.20734   0.20029  0.19
cond_oc_dum_2inact            -0.10678   0.89872  0.30182   0.23316 -0.46
cond_oc_dum_3unemp            -0.00802   0.99202  0.30611   0.23418 -0.03
susprindum_oh                  0.02049   1.02070  0.52190   0.40816  0.05
susprindum_coc                 0.24730   1.28057  0.53311   0.42121  0.59
susprindum_pbc                 0.05301   1.05444  0.48619   0.38701  0.14
susprindum_mar                -0.01869   0.98148  0.77774   0.59397 -0.03
                              Pr(>|z|)   
tr_outcome.lag                  0.5227   
less_90d_tr1.lag                0.7331   
log_dias_treat_imp_sin_na.lag   0.0260 * 
comp_bpsc_y3_severe.lag         0.3761   
policonsumo2.lag                0.3276   
edad_al_ing_1                   0.5607   
ano_nac_corr                    0.3995   
susinidum_oh                    0.0527 . 
susinidum_coc                   0.2685   
susinidum_pbc                   0.0996 . 
susinidum_mar                   0.3805   
psycom_dum_with                 0.0074 **
psycom_dum_study                0.0165 * 
freq_cons_dum_5day              0.8486   
cond_oc_dum_2inact              0.6470   
cond_oc_dum_3unemp              0.9727   
susprindum_oh                   0.9600   
susprindum_coc                  0.5571   
susprindum_pbc                  0.8910   
susprindum_mar                  0.9749   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    0.710      1.408     0.248     2.029
less_90d_tr1.lag                  1.237      0.809     0.365     4.195
log_dias_treat_imp_sin_na.lag     0.579      1.728     0.358     0.937
comp_bpsc_y3_severe.lag           1.843      0.543     0.476     7.133
policonsumo2.lag                  1.458      0.686     0.685     3.102
edad_al_ing_1                     1.034      0.968     0.925     1.155
ano_nac_corr                      1.048      0.955     0.940     1.168
susinidum_oh                      0.420      2.380     0.175     1.010
susinidum_coc                     0.563      1.775     0.204     1.557
susinidum_pbc                     0.465      2.150     0.187     1.157
susinidum_mar                     0.660      1.516     0.261     1.671
psycom_dum_with                   0.562      1.779     0.369     0.857
psycom_dum_study                  0.523      1.913     0.308     0.889
freq_cons_dum_5day                1.039      0.962     0.702     1.538
cond_oc_dum_2inact                0.899      1.113     0.569     1.419
cond_oc_dum_3unemp                0.992      1.008     0.627     1.570
susprindum_oh                     1.021      0.980     0.459     2.272
susprindum_coc                    1.281      0.781     0.561     2.924
susprindum_pbc                    1.054      0.948     0.494     2.251
susprindum_mar                    0.981      1.019     0.306     3.144

Concordance= 0.606  (se = 0.025 )
Likelihood ratio test= 31.1  on 20 df,   p=0.05
Wald test            = 49.6  on 20 df,   p=3e-04
Score (logrank) test = 29  on 20 df,   p=0.09,   Robust = 33.7  p=0.03

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   1.00    2.17    2.63    3.28    3.73   23.21 
[1] "Modality: GP intensive ambulatory\nTime interval: (50,70]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 952, number of events= 952 
   (882 observations deleted due to missingness)

                                  coef exp(coef) se(coef) robust se     z
tr_outcome.lag                 0.15073   1.16268  0.35885   0.20378  0.74
less_90d_tr1.lag              -0.12136   0.88571  0.31729   0.20318 -0.60
log_dias_treat_imp_sin_na.lag -0.11381   0.89243  0.12990   0.08126 -1.40
comp_bpsc_y3_severe.lag        0.18725   1.20593  0.24138   0.15731  1.19
policonsumo2.lag              -0.11770   0.88896  0.36364   0.21269 -0.55
edad_al_ing_1                  0.09190   1.09625  0.02330   0.02268  4.05
ano_nac_corr                   0.08750   1.09144  0.02299   0.02209  3.96
susinidum_oh                  -0.17827   0.83671  0.18565   0.18775 -0.95
susinidum_coc                 -0.00593   0.99409  0.23334   0.25203 -0.02
susinidum_pbc                  0.09783   1.10277  0.22926   0.22434  0.44
susinidum_mar                 -0.05800   0.94365  0.19141   0.19611 -0.30
psycom_dum_with                0.11281   1.11942  0.07412   0.07576  1.49
psycom_dum_study               0.07157   1.07419  0.09999   0.09527  0.75
freq_cons_dum_5day             0.03258   1.03312  0.06785   0.06701  0.49
cond_oc_dum_2inact             0.08914   1.09324  0.10393   0.10346  0.86
cond_oc_dum_3unemp            -0.00713   0.99289  0.07171   0.07102 -0.10
susprindum_oh                 -0.33803   0.71318  0.30827   0.26428 -1.28
susprindum_coc                -0.49815   0.60766  0.31335   0.27108 -1.84
susprindum_pbc                -0.35665   0.70002  0.30391   0.26104 -1.37
susprindum_mar                -0.37735   0.68567  0.32531   0.29473 -1.28
                              Pr(>|z|)    
tr_outcome.lag                   0.460    
less_90d_tr1.lag                 0.550    
log_dias_treat_imp_sin_na.lag    0.161    
comp_bpsc_y3_severe.lag          0.234    
policonsumo2.lag                 0.580    
edad_al_ing_1                  5.1e-05 ***
ano_nac_corr                   7.5e-05 ***
susinidum_oh                     0.342    
susinidum_coc                    0.981    
susinidum_pbc                    0.663    
susinidum_mar                    0.767    
psycom_dum_with                  0.137    
psycom_dum_study                 0.453    
freq_cons_dum_5day               0.627    
cond_oc_dum_2inact               0.389    
cond_oc_dum_3unemp               0.920    
susprindum_oh                    0.201    
susprindum_coc                   0.066 .  
susprindum_pbc                   0.172    
susprindum_mar                   0.200    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    1.163      0.860     0.780      1.73
less_90d_tr1.lag                  0.886      1.129     0.595      1.32
log_dias_treat_imp_sin_na.lag     0.892      1.121     0.761      1.05
comp_bpsc_y3_severe.lag           1.206      0.829     0.886      1.64
policonsumo2.lag                  0.889      1.125     0.586      1.35
edad_al_ing_1                     1.096      0.912     1.049      1.15
ano_nac_corr                      1.091      0.916     1.045      1.14
susinidum_oh                      0.837      1.195     0.579      1.21
susinidum_coc                     0.994      1.006     0.607      1.63
susinidum_pbc                     1.103      0.907     0.710      1.71
susinidum_mar                     0.944      1.060     0.643      1.39
psycom_dum_with                   1.119      0.893     0.965      1.30
psycom_dum_study                  1.074      0.931     0.891      1.29
freq_cons_dum_5day                1.033      0.968     0.906      1.18
cond_oc_dum_2inact                1.093      0.915     0.893      1.34
cond_oc_dum_3unemp                0.993      1.007     0.864      1.14
susprindum_oh                     0.713      1.402     0.425      1.20
susprindum_coc                    0.608      1.646     0.357      1.03
susprindum_pbc                    0.700      1.429     0.420      1.17
susprindum_mar                    0.686      1.458     0.385      1.22

Concordance= 0.563  (se = 0.01 )
Likelihood ratio test= 35.7  on 20 df,   p=0.02
Wald test            = 44.3  on 20 df,   p=0.001
Score (logrank) test = 35.4  on 20 df,   p=0.02,   Robust = 41.4  p=0.003

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.81    1.28    1.46    1.50    1.68    3.37 
[1] "Modality: basic ambulatory\nTime interval: (50,70]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 975, number of events= 975 
   (912 observations deleted due to missingness)

                                 coef exp(coef) se(coef) robust se     z
tr_outcome.lag                -0.1636    0.8491   0.2844    0.2281 -0.72
less_90d_tr1.lag              -0.4111    0.6629   0.3383    0.2087 -1.97
log_dias_treat_imp_sin_na.lag -0.1009    0.9041   0.0857    0.0621 -1.62
comp_bpsc_y3_severe.lag        0.2623    1.3000   0.2522    0.1943  1.35
policonsumo2.lag               0.0830    1.0866   0.2859    0.2368  0.35
edad_al_ing_1                  0.1263    1.1346   0.0210    0.0197  6.41
ano_nac_corr                   0.1315    1.1406   0.0209    0.0198  6.64
susinidum_oh                  -0.0636    0.9384   0.1742    0.2007 -0.32
susinidum_coc                 -0.3028    0.7387   0.2403    0.2725 -1.11
susinidum_pbc                 -0.1116    0.8944   0.2203    0.2294 -0.49
susinidum_mar                 -0.0607    0.9411   0.1806    0.2032 -0.30
psycom_dum_with               -0.1183    0.8884   0.0739    0.0733 -1.61
psycom_dum_study              -0.0289    0.9715   0.0939    0.0925 -0.31
freq_cons_dum_5day             0.0824    1.0859   0.0754    0.0759  1.09
cond_oc_dum_2inact             0.1289    1.1376   0.1038    0.1014  1.27
cond_oc_dum_3unemp             0.0419    1.0428   0.0784    0.0761  0.55
susprindum_oh                  0.2948    1.3428   0.2325    0.2324  1.27
susprindum_coc                 0.2484    1.2820   0.2364    0.2344  1.06
susprindum_pbc                 0.1830    1.2009   0.2330    0.2318  0.79
susprindum_mar                 0.0408    1.0416   0.2557    0.2599  0.16
                              Pr(>|z|)    
tr_outcome.lag                   0.473    
less_90d_tr1.lag                 0.049 *  
log_dias_treat_imp_sin_na.lag    0.104    
comp_bpsc_y3_severe.lag          0.177    
policonsumo2.lag                 0.726    
edad_al_ing_1                  1.4e-10 ***
ano_nac_corr                   3.1e-11 ***
susinidum_oh                     0.751    
susinidum_coc                    0.267    
susinidum_pbc                    0.627    
susinidum_mar                    0.765    
psycom_dum_with                  0.107    
psycom_dum_study                 0.754    
freq_cons_dum_5day               0.278    
cond_oc_dum_2inact               0.204    
cond_oc_dum_3unemp               0.582    
susprindum_oh                    0.205    
susprindum_coc                   0.289    
susprindum_pbc                   0.430    
susprindum_mar                   0.875    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    0.849      1.178     0.543     1.328
less_90d_tr1.lag                  0.663      1.508     0.440     0.998
log_dias_treat_imp_sin_na.lag     0.904      1.106     0.800     1.021
comp_bpsc_y3_severe.lag           1.300      0.769     0.888     1.903
policonsumo2.lag                  1.087      0.920     0.683     1.728
edad_al_ing_1                     1.135      0.881     1.092     1.179
ano_nac_corr                      1.141      0.877     1.097     1.186
susinidum_oh                      0.938      1.066     0.633     1.391
susinidum_coc                     0.739      1.354     0.433     1.260
susinidum_pbc                     0.894      1.118     0.570     1.402
susinidum_mar                     0.941      1.063     0.632     1.401
psycom_dum_with                   0.888      1.126     0.770     1.026
psycom_dum_study                  0.971      1.029     0.810     1.164
freq_cons_dum_5day                1.086      0.921     0.936     1.260
cond_oc_dum_2inact                1.138      0.879     0.932     1.388
cond_oc_dum_3unemp                1.043      0.959     0.898     1.211
susprindum_oh                     1.343      0.745     0.852     2.118
susprindum_coc                    1.282      0.780     0.810     2.030
susprindum_pbc                    1.201      0.833     0.762     1.891
susprindum_mar                    1.042      0.960     0.626     1.734

Concordance= 0.578  (se = 0.01 )
Likelihood ratio test= 66  on 20 df,   p=8e-07
Wald test            = 70.7  on 20 df,   p=1e-07
Score (logrank) test = 65.2  on 20 df,   p=1e-06,   Robust = 71  p=1e-07

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.468   0.703   0.866   0.903   1.059   2.559 
[1] "Modality: GP residential\nTime interval: (50,70]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 392, number of events= 392 
   (361 observations deleted due to missingness)

                                 coef exp(coef) se(coef) robust se      z
tr_outcome.lag                -0.9365    0.3920   0.4568    0.3123  -3.00
less_90d_tr1.lag               0.4865    1.6266   0.5871    0.4173   1.17
log_dias_treat_imp_sin_na.lag  0.0244    1.0247   0.1835    0.1265   0.19
comp_bpsc_y3_severe.lag       -0.2099    0.8107   0.5023    0.3620  -0.58
policonsumo2.lag               0.2445    1.2769   0.6041    0.4989   0.49
edad_al_ing_1                  0.0888    1.0928   0.0377    0.0376   2.36
ano_nac_corr                   0.1002    1.1053   0.0376    0.0379   2.64
susinidum_oh                   0.3006    1.3507   0.2867    0.2624   1.15
susinidum_coc                  0.2230    1.2498   0.3793    0.3298   0.68
susinidum_pbc                  0.1229    1.1308   0.3167    0.2983   0.41
susinidum_mar                  0.0527    1.0541   0.2870    0.2583   0.20
psycom_dum_with               -0.2135    0.8077   0.1207    0.1185  -1.80
psycom_dum_study              -0.1890    0.8278   0.1750    0.1622  -1.17
freq_cons_dum_5day            -0.1111    0.8948   0.1189    0.1213  -0.92
cond_oc_dum_2inact             0.0119    1.0120   0.2003    0.1955   0.06
cond_oc_dum_3unemp            -0.1085    0.8972   0.1560    0.1579  -0.69
susprindum_oh                 -1.6833    0.1858   0.7330    0.2015  -8.35
susprindum_coc                -2.0533    0.1283   0.7406    0.2254  -9.11
susprindum_pbc                -1.8949    0.1503   0.7271    0.1580 -11.99
susprindum_mar                -1.9872    0.1371   0.8088    0.4221  -4.71
                              Pr(>|z|)    
tr_outcome.lag                  0.0027 ** 
less_90d_tr1.lag                0.2437    
log_dias_treat_imp_sin_na.lag   0.8471    
comp_bpsc_y3_severe.lag         0.5621    
policonsumo2.lag                0.6241    
edad_al_ing_1                   0.0183 *  
ano_nac_corr                    0.0083 ** 
susinidum_oh                    0.2520    
susinidum_coc                   0.4989    
susinidum_pbc                   0.6803    
susinidum_mar                   0.8384    
psycom_dum_with                 0.0715 .  
psycom_dum_study                0.2438    
freq_cons_dum_5day              0.3595    
cond_oc_dum_2inact              0.9514    
cond_oc_dum_3unemp              0.4920    
susprindum_oh                  < 2e-16 ***
susprindum_coc                 < 2e-16 ***
susprindum_pbc                 < 2e-16 ***
susprindum_mar                 2.5e-06 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    0.392      2.551    0.2125     0.723
less_90d_tr1.lag                  1.627      0.615    0.7179     3.685
log_dias_treat_imp_sin_na.lag     1.025      0.976    0.7997     1.313
comp_bpsc_y3_severe.lag           0.811      1.234    0.3987     1.648
policonsumo2.lag                  1.277      0.783    0.4803     3.395
edad_al_ing_1                     1.093      0.915    1.0152     1.176
ano_nac_corr                      1.105      0.905    1.0261     1.191
susinidum_oh                      1.351      0.740    0.8076     2.259
susinidum_coc                     1.250      0.800    0.6548     2.386
susinidum_pbc                     1.131      0.884    0.6301     2.029
susinidum_mar                     1.054      0.949    0.6353     1.749
psycom_dum_with                   0.808      1.238    0.6404     1.019
psycom_dum_study                  0.828      1.208    0.6024     1.138
freq_cons_dum_5day                0.895      1.118    0.7055     1.135
cond_oc_dum_2inact                1.012      0.988    0.6899     1.484
cond_oc_dum_3unemp                0.897      1.115    0.6583     1.223
susprindum_oh                     0.186      5.384    0.1252     0.276
susprindum_coc                    0.128      7.794    0.0825     0.200
susprindum_pbc                    0.150      6.652    0.1103     0.205
susprindum_mar                    0.137      7.295    0.0599     0.314

Concordance= 0.578  (se = 0.017 )
Likelihood ratio test= 33.6  on 20 df,   p=0.03
Wald test            = 228  on 20 df,   p=<2e-16
Score (logrank) test = 35  on 20 df,   p=0.02,   Robust = 32.5  p=0.04

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   1.40    6.41    7.57    8.01    8.96   24.67 
[1] "Modality: WO intensive ambulatory\nTime interval: (50,70]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 147, number of events= 147 
   (135 observations deleted due to missingness)

                                 coef exp(coef) se(coef) robust se     z
tr_outcome.lag                 2.1717    8.7728   0.9544    0.7971  2.72
less_90d_tr1.lag               1.2465    3.4781   1.2201    0.7444  1.67
log_dias_treat_imp_sin_na.lag -0.2393    0.7872   0.1925    0.1025 -2.33
comp_bpsc_y3_severe.lag        2.0365    7.6639   1.0136    0.6358  3.20
policonsumo2.lag              -2.9230    0.0538   1.2871    0.7707 -3.79
edad_al_ing_1                  0.1148    1.1217   0.0580    0.0543  2.11
ano_nac_corr                   0.1032    1.1087   0.0557    0.0526  1.96
susinidum_oh                  -0.3817    0.6827   0.6662    0.3756 -1.02
susinidum_coc                  0.0769    1.0800   0.8669    0.7215  0.11
susinidum_pbc                 -0.6811    0.5060   0.7061    0.4589 -1.48
susinidum_mar                 -0.3223    0.7245   0.6791    0.4243 -0.76
psycom_dum_with               -0.0156    0.9845   0.2213    0.2287 -0.07
psycom_dum_study               0.2414    1.2730   0.3154    0.3129  0.77
freq_cons_dum_5day            -0.0729    0.9297   0.1940    0.1803 -0.40
cond_oc_dum_2inact             0.1242    1.1323   0.2438    0.2646  0.47
cond_oc_dum_3unemp            -0.0392    0.9615   0.2570    0.2860 -0.14
susprindum_oh                 -0.5190    0.5951   0.7758    0.3672 -1.41
susprindum_coc                -0.7877    0.4549   0.8090    0.5286 -1.49
susprindum_pbc                -0.3925    0.6754   0.7660    0.3953 -0.99
susprindum_mar                -1.0265    0.3583   0.8136    0.4512 -2.28
                              Pr(>|z|)    
tr_outcome.lag                 0.00644 ** 
less_90d_tr1.lag               0.09406 .  
log_dias_treat_imp_sin_na.lag  0.01957 *  
comp_bpsc_y3_severe.lag        0.00136 ** 
policonsumo2.lag               0.00015 ***
edad_al_ing_1                  0.03457 *  
ano_nac_corr                   0.04983 *  
susinidum_oh                   0.30951    
susinidum_coc                  0.91510    
susinidum_pbc                  0.13773    
susinidum_mar                  0.44753    
psycom_dum_with                0.94560    
psycom_dum_study               0.44047    
freq_cons_dum_5day             0.68583    
cond_oc_dum_2inact             0.63867    
cond_oc_dum_3unemp             0.89088    
susprindum_oh                  0.15756    
susprindum_coc                 0.13619    
susprindum_pbc                 0.32072    
susprindum_mar                 0.02290 *  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                   8.7728      0.114    1.8393    41.844
less_90d_tr1.lag                 3.4781      0.288    0.8085    14.963
log_dias_treat_imp_sin_na.lag    0.7872      1.270    0.6439     0.962
comp_bpsc_y3_severe.lag          7.6639      0.130    2.2042    26.647
policonsumo2.lag                 0.0538     18.597    0.0119     0.244
edad_al_ing_1                    1.1217      0.892    1.0084     1.248
ano_nac_corr                     1.1087      0.902    1.0001     1.229
susinidum_oh                     0.6827      1.465    0.3270     1.425
susinidum_coc                    1.0800      0.926    0.2626     4.441
susinidum_pbc                    0.5060      1.976    0.2059     1.244
susinidum_mar                    0.7245      1.380    0.3154     1.664
psycom_dum_with                  0.9845      1.016    0.6288     1.541
psycom_dum_study                 1.2730      0.786    0.6894     2.351
freq_cons_dum_5day               0.9297      1.076    0.6529     1.324
cond_oc_dum_2inact               1.1323      0.883    0.6742     1.902
cond_oc_dum_3unemp               0.9615      1.040    0.5489     1.684
susprindum_oh                    0.5951      1.680    0.2897     1.222
susprindum_coc                   0.4549      2.198    0.1614     1.282
susprindum_pbc                   0.6754      1.481    0.3112     1.466
susprindum_mar                   0.3583      2.791    0.1480     0.867

Concordance= 0.616  (se = 0.024 )
Likelihood ratio test= 30.5  on 20 df,   p=0.06
Wald test            = 95.8  on 20 df,   p=7e-12
Score (logrank) test = 28.2  on 20 df,   p=0.1,   Robust = 27.9  p=0.1

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.01    1.75    2.26    2.72    2.77   26.75 
[1] "Modality: WO residential\nTime interval: (70,135]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 140, number of events= 140 
   (114 observations deleted due to missingness)

                                 coef exp(coef) se(coef) robust se     z
tr_outcome.lag                 0.1218    1.1296   0.4994    0.3683  0.33
less_90d_tr1.lag              -0.3431    0.7096   0.6058    0.3729 -0.92
log_dias_treat_imp_sin_na.lag -0.1413    0.8683   0.1374    0.1287 -1.10
comp_bpsc_y3_severe.lag       -0.0120    0.9881   0.5646    0.3978 -0.03
policonsumo2.lag               0.3567    1.4286   0.6815    0.5466  0.65
edad_al_ing_1                  0.7500    2.1170   0.1299    0.1256  5.97
ano_nac_corr                   0.7538    2.1251   0.1269    0.1211  6.22
susinidum_oh                  -0.1588    0.8532   0.7420    0.6603 -0.24
susinidum_coc                  0.2858    1.3308   0.9144    0.8027  0.36
susinidum_pbc                  0.0273    1.0277   0.7797    0.7058  0.04
susinidum_mar                 -0.4422    0.6426   0.7490    0.6758 -0.65
psycom_dum_with                0.2162    1.2413   0.2481    0.2176  0.99
psycom_dum_study               0.3097    1.3631   0.3015    0.2705  1.15
freq_cons_dum_5day            -0.1209    0.8861   0.2272    0.2189 -0.55
cond_oc_dum_2inact             0.3602    1.4335   0.3818    0.2755  1.31
cond_oc_dum_3unemp             0.3172    1.3732   0.3740    0.2661  1.19
susprindum_oh                 -0.8760    0.4165   0.6861    0.5430 -1.61
susprindum_coc                -0.7450    0.4747   0.6871    0.5893 -1.26
susprindum_pbc                -0.8698    0.4190   0.6718    0.5723 -1.52
susprindum_mar                -0.0988    0.9060   0.8162    0.7142 -0.14
                              Pr(>|z|)    
tr_outcome.lag                    0.74    
less_90d_tr1.lag                  0.36    
log_dias_treat_imp_sin_na.lag     0.27    
comp_bpsc_y3_severe.lag           0.98    
policonsumo2.lag                  0.51    
edad_al_ing_1                  2.3e-09 ***
ano_nac_corr                   4.9e-10 ***
susinidum_oh                      0.81    
susinidum_coc                     0.72    
susinidum_pbc                     0.97    
susinidum_mar                     0.51    
psycom_dum_with                   0.32    
psycom_dum_study                  0.25    
freq_cons_dum_5day                0.58    
cond_oc_dum_2inact                0.19    
cond_oc_dum_3unemp                0.23    
susprindum_oh                     0.11    
susprindum_coc                    0.21    
susprindum_pbc                    0.13    
susprindum_mar                    0.89    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    1.130      0.885     0.549      2.32
less_90d_tr1.lag                  0.710      1.409     0.342      1.47
log_dias_treat_imp_sin_na.lag     0.868      1.152     0.675      1.12
comp_bpsc_y3_severe.lag           0.988      1.012     0.453      2.16
policonsumo2.lag                  1.429      0.700     0.489      4.17
edad_al_ing_1                     2.117      0.472     1.655      2.71
ano_nac_corr                      2.125      0.471     1.676      2.69
susinidum_oh                      0.853      1.172     0.234      3.11
susinidum_coc                     1.331      0.751     0.276      6.42
susinidum_pbc                     1.028      0.973     0.258      4.10
susinidum_mar                     0.643      1.556     0.171      2.42
psycom_dum_with                   1.241      0.806     0.810      1.90
psycom_dum_study                  1.363      0.734     0.802      2.32
freq_cons_dum_5day                0.886      1.129     0.577      1.36
cond_oc_dum_2inact                1.434      0.698     0.835      2.46
cond_oc_dum_3unemp                1.373      0.728     0.815      2.31
susprindum_oh                     0.416      2.401     0.144      1.21
susprindum_coc                    0.475      2.106     0.150      1.51
susprindum_pbc                    0.419      2.386     0.136      1.29
susprindum_mar                    0.906      1.104     0.223      3.67

Concordance= 0.689  (se = 0.021 )
Likelihood ratio test= 59.3  on 20 df,   p=9e-06
Wald test            = 97  on 20 df,   p=4e-12
Score (logrank) test = 66  on 20 df,   p=8e-07,   Robust = 66.5  p=7e-07

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.22    0.97    2.11    2.33    3.15    8.03 
[1] "Modality: GP intensive ambulatory\nTime interval: (70,135]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 747, number of events= 747 
   (659 observations deleted due to missingness)

                                 coef exp(coef) se(coef) robust se     z
tr_outcome.lag                -0.2388    0.7875   0.2250    0.1650 -1.45
less_90d_tr1.lag               0.0671    1.0694   0.3007    0.2596  0.26
log_dias_treat_imp_sin_na.lag -0.0766    0.9263   0.0842    0.0612 -1.25
comp_bpsc_y3_severe.lag        0.1116    1.1180   0.2199    0.1661  0.67
policonsumo2.lag               0.2374    1.2680   0.2585    0.1697  1.40
edad_al_ing_1                  0.5374    1.7116   0.0388    0.0361 14.88
ano_nac_corr                   0.5364    1.7098   0.0388    0.0359 14.95
susinidum_oh                  -0.0780    0.9250   0.2302    0.2226 -0.35
susinidum_coc                  0.0180    1.0182   0.2923    0.2846  0.06
susinidum_pbc                  0.0170    1.0171   0.2861    0.2684  0.06
susinidum_mar                  0.0206    1.0208   0.2366    0.2294  0.09
psycom_dum_with               -0.1143    0.8920   0.0833    0.0801 -1.43
psycom_dum_study              -0.0224    0.9779   0.1145    0.1047 -0.21
freq_cons_dum_5day            -0.0159    0.9842   0.0788    0.0728 -0.22
cond_oc_dum_2inact             0.0776    1.0807   0.1227    0.1112  0.70
cond_oc_dum_3unemp             0.0321    1.0326   0.0832    0.0774  0.41
susprindum_oh                 -0.0722    0.9304   0.3341    0.1880 -0.38
susprindum_coc                -0.0727    0.9299   0.3364    0.1937 -0.38
susprindum_pbc                -0.1282    0.8797   0.3291    0.1787 -0.72
susprindum_mar                -0.0377    0.9630   0.3534    0.2304 -0.16
                              Pr(>|z|)    
tr_outcome.lag                    0.15    
less_90d_tr1.lag                  0.80    
log_dias_treat_imp_sin_na.lag     0.21    
comp_bpsc_y3_severe.lag           0.50    
policonsumo2.lag                  0.16    
edad_al_ing_1                   <2e-16 ***
ano_nac_corr                    <2e-16 ***
susinidum_oh                      0.73    
susinidum_coc                     0.95    
susinidum_pbc                     0.95    
susinidum_mar                     0.93    
psycom_dum_with                   0.15    
psycom_dum_study                  0.83    
freq_cons_dum_5day                0.83    
cond_oc_dum_2inact                0.49    
cond_oc_dum_3unemp                0.68    
susprindum_oh                     0.70    
susprindum_coc                    0.71    
susprindum_pbc                    0.47    
susprindum_mar                    0.87    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    0.788      1.270     0.570      1.09
less_90d_tr1.lag                  1.069      0.935     0.643      1.78
log_dias_treat_imp_sin_na.lag     0.926      1.080     0.822      1.04
comp_bpsc_y3_severe.lag           1.118      0.894     0.807      1.55
policonsumo2.lag                  1.268      0.789     0.909      1.77
edad_al_ing_1                     1.712      0.584     1.595      1.84
ano_nac_corr                      1.710      0.585     1.594      1.83
susinidum_oh                      0.925      1.081     0.598      1.43
susinidum_coc                     1.018      0.982     0.583      1.78
susinidum_pbc                     1.017      0.983     0.601      1.72
susinidum_mar                     1.021      0.980     0.651      1.60
psycom_dum_with                   0.892      1.121     0.762      1.04
psycom_dum_study                  0.978      1.023     0.796      1.20
freq_cons_dum_5day                0.984      1.016     0.853      1.14
cond_oc_dum_2inact                1.081      0.925     0.869      1.34
cond_oc_dum_3unemp                1.033      0.968     0.887      1.20
susprindum_oh                     0.930      1.075     0.644      1.34
susprindum_coc                    0.930      1.075     0.636      1.36
susprindum_pbc                    0.880      1.137     0.620      1.25
susprindum_mar                    0.963      1.038     0.613      1.51

Concordance= 0.642  (se = 0.011 )
Likelihood ratio test= 213  on 20 df,   p=<2e-16
Wald test            = 269  on 20 df,   p=<2e-16
Score (logrank) test = 216  on 20 df,   p=<2e-16,   Robust = 195  p=<2e-16

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.19    0.77    1.29    1.46    1.93    6.42 
[1] "Modality: basic ambulatory\nTime interval: (70,135]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 699, number of events= 699 
   (617 observations deleted due to missingness)

                                 coef exp(coef) se(coef) robust se     z
tr_outcome.lag                 0.1225    1.1304   0.2986    0.2606  0.47
less_90d_tr1.lag              -0.1555    0.8560   0.3452    0.2417 -0.64
log_dias_treat_imp_sin_na.lag -0.1626    0.8499   0.0931    0.0629 -2.58
comp_bpsc_y3_severe.lag        0.5189    1.6802   0.2279    0.2013  2.58
policonsumo2.lag               0.0739    1.0767   0.3118    0.2749  0.27
edad_al_ing_1                  0.5115    1.6679   0.0417    0.0412 12.41
ano_nac_corr                   0.5102    1.6655   0.0414    0.0410 12.44
susinidum_oh                  -0.1429    0.8668   0.2422    0.2256 -0.63
susinidum_coc                 -0.3566    0.7000   0.2801    0.2538 -1.40
susinidum_pbc                 -0.1280    0.8798   0.2786    0.2567 -0.50
susinidum_mar                 -0.1350    0.8737   0.2499    0.2303 -0.59
psycom_dum_with                0.0772    1.0803   0.0868    0.0828  0.93
psycom_dum_study              -0.0475    0.9536   0.1097    0.0992 -0.48
freq_cons_dum_5day            -0.0150    0.9851   0.0881    0.0823 -0.18
cond_oc_dum_2inact             0.1047    1.1104   0.1201    0.1228  0.85
cond_oc_dum_3unemp            -0.1221    0.8850   0.0954    0.0850 -1.44
susprindum_oh                 -0.2467    0.7814   0.3190    0.2537 -0.97
susprindum_coc                -0.1435    0.8663   0.3174    0.2495 -0.57
susprindum_pbc                -0.1953    0.8226   0.3134    0.2427 -0.80
susprindum_mar                -0.0947    0.9097   0.3344    0.2619 -0.36
                              Pr(>|z|)    
tr_outcome.lag                  0.6382    
less_90d_tr1.lag                0.5201    
log_dias_treat_imp_sin_na.lag   0.0098 ** 
comp_bpsc_y3_severe.lag         0.0100 ** 
policonsumo2.lag                0.7881    
edad_al_ing_1                   <2e-16 ***
ano_nac_corr                    <2e-16 ***
susinidum_oh                    0.5265    
susinidum_coc                   0.1601    
susinidum_pbc                   0.6180    
susinidum_mar                   0.5576    
psycom_dum_with                 0.3511    
psycom_dum_study                0.6320    
freq_cons_dum_5day              0.8557    
cond_oc_dum_2inact              0.3939    
cond_oc_dum_3unemp              0.1507    
susprindum_oh                   0.3310    
susprindum_coc                  0.5653    
susprindum_pbc                  0.4210    
susprindum_mar                  0.7177    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    1.130      0.885     0.678     1.884
less_90d_tr1.lag                  0.856      1.168     0.533     1.375
log_dias_treat_imp_sin_na.lag     0.850      1.177     0.751     0.962
comp_bpsc_y3_severe.lag           1.680      0.595     1.132     2.493
policonsumo2.lag                  1.077      0.929     0.628     1.845
edad_al_ing_1                     1.668      0.600     1.538     1.808
ano_nac_corr                      1.666      0.600     1.537     1.805
susinidum_oh                      0.867      1.154     0.557     1.349
susinidum_coc                     0.700      1.428     0.426     1.151
susinidum_pbc                     0.880      1.137     0.532     1.455
susinidum_mar                     0.874      1.145     0.556     1.372
psycom_dum_with                   1.080      0.926     0.918     1.271
psycom_dum_study                  0.954      1.049     0.785     1.158
freq_cons_dum_5day                0.985      1.015     0.838     1.158
cond_oc_dum_2inact                1.110      0.901     0.873     1.413
cond_oc_dum_3unemp                0.885      1.130     0.749     1.045
susprindum_oh                     0.781      1.280     0.475     1.285
susprindum_coc                    0.866      1.154     0.531     1.413
susprindum_pbc                    0.823      1.216     0.511     1.324
susprindum_mar                    0.910      1.099     0.544     1.520

Concordance= 0.626  (se = 0.012 )
Likelihood ratio test= 168  on 20 df,   p=<2e-16
Wald test            = 186  on 20 df,   p=<2e-16
Score (logrank) test = 168  on 20 df,   p=<2e-16,   Robust = 154  p=<2e-16

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.25    0.88    1.42    1.60    2.05    8.41 
[1] "Modality: GP residential\nTime interval: (70,135]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 325, number of events= 325 
   (273 observations deleted due to missingness)

                                 coef exp(coef) se(coef) robust se     z
tr_outcome.lag                -0.0928    0.9113   0.3333    0.2138 -0.43
less_90d_tr1.lag               0.0753    1.0782   0.3822    0.2800  0.27
log_dias_treat_imp_sin_na.lag -0.0724    0.9302   0.1271    0.1081 -0.67
comp_bpsc_y3_severe.lag        0.2079    1.2311   0.3464    0.2177  0.95
policonsumo2.lag              -0.3087    0.7344   0.3645    0.2956 -1.04
edad_al_ing_1                  0.6032    1.8279   0.0654    0.0575 10.49
ano_nac_corr                   0.6012    1.8243   0.0655    0.0572 10.51
susinidum_oh                  -0.5745    0.5630   0.3975    0.2152 -2.67
susinidum_coc                 -0.5009    0.6060   0.4717    0.2745 -1.82
susinidum_pbc                 -0.6329    0.5310   0.4197    0.2457 -2.58
susinidum_mar                 -0.7080    0.4926   0.4014    0.2244 -3.16
psycom_dum_with                0.0821    1.0856   0.1289    0.1197  0.69
psycom_dum_study               0.1134    1.1201   0.1851    0.1634  0.69
freq_cons_dum_5day            -0.1383    0.8709   0.1261    0.1162 -1.19
cond_oc_dum_2inact            -0.3026    0.7389   0.2316    0.2019 -1.50
cond_oc_dum_3unemp            -0.3872    0.6789   0.1644    0.1483 -2.61
susprindum_oh                  0.1674    1.1822   0.7395    0.3219  0.52
susprindum_coc                 0.2205    1.2467   0.7360    0.3168  0.70
susprindum_pbc                 0.3005    1.3506   0.7227    0.2687  1.12
susprindum_mar                 0.3069    1.3591   0.8012    0.3779  0.81
                              Pr(>|z|)    
tr_outcome.lag                  0.6641    
less_90d_tr1.lag                0.7879    
log_dias_treat_imp_sin_na.lag   0.5032    
comp_bpsc_y3_severe.lag         0.3397    
policonsumo2.lag                0.2964    
edad_al_ing_1                   <2e-16 ***
ano_nac_corr                    <2e-16 ***
susinidum_oh                    0.0076 ** 
susinidum_coc                   0.0681 .  
susinidum_pbc                   0.0100 *  
susinidum_mar                   0.0016 ** 
psycom_dum_with                 0.4926    
psycom_dum_study                0.4876    
freq_cons_dum_5day              0.2340    
cond_oc_dum_2inact              0.1339    
cond_oc_dum_3unemp              0.0090 ** 
susprindum_oh                   0.6031    
susprindum_coc                  0.4865    
susprindum_pbc                  0.2634    
susprindum_mar                  0.4167    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    0.911      1.097     0.599     1.386
less_90d_tr1.lag                  1.078      0.927     0.623     1.867
log_dias_treat_imp_sin_na.lag     0.930      1.075     0.752     1.150
comp_bpsc_y3_severe.lag           1.231      0.812     0.803     1.886
policonsumo2.lag                  0.734      1.362     0.411     1.311
edad_al_ing_1                     1.828      0.547     1.633     2.046
ano_nac_corr                      1.824      0.548     1.631     2.041
susinidum_oh                      0.563      1.776     0.369     0.858
susinidum_coc                     0.606      1.650     0.354     1.038
susinidum_pbc                     0.531      1.883     0.328     0.860
susinidum_mar                     0.493      2.030     0.317     0.765
psycom_dum_with                   1.086      0.921     0.859     1.373
psycom_dum_study                  1.120      0.893     0.813     1.543
freq_cons_dum_5day                0.871      1.148     0.694     1.094
cond_oc_dum_2inact                0.739      1.353     0.497     1.098
cond_oc_dum_3unemp                0.679      1.473     0.508     0.908
susprindum_oh                     1.182      0.846     0.629     2.222
susprindum_coc                    1.247      0.802     0.670     2.320
susprindum_pbc                    1.351      0.740     0.798     2.287
susprindum_mar                    1.359      0.736     0.648     2.850

Concordance= 0.651  (se = 0.015 )
Likelihood ratio test= 96.8  on 20 df,   p=5e-12
Wald test            = 144  on 20 df,   p=<2e-16
Score (logrank) test = 100  on 20 df,   p=1e-12,   Robust = 103  p=4e-13

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.24    1.32    2.16    2.50    3.33    8.22 
[1] "Modality: WO intensive ambulatory\nTime interval: (70,135]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 109, number of events= 109 
   (89 observations deleted due to missingness)

                                 coef exp(coef) se(coef) robust se     z
tr_outcome.lag                 0.6955    2.0047   0.6932    0.5721  1.22
less_90d_tr1.lag               0.5006    1.6497   0.9757    0.8386  0.60
log_dias_treat_imp_sin_na.lag -0.0471    0.9540   0.1707    0.1345 -0.35
comp_bpsc_y3_severe.lag       -0.4559    0.6339   0.6622    0.5470 -0.83
policonsumo2.lag              -1.0245    0.3590   0.6121    0.4678 -2.19
edad_al_ing_1                  0.7246    2.0639   0.1274    0.1228  5.90
ano_nac_corr                   0.7012    2.0162   0.1237    0.1211  5.79
susinidum_oh                   0.1961    1.2167   1.1315    0.4793  0.41
susinidum_coc                 -1.3926    0.2484   1.5539    0.5582 -2.49
susinidum_pbc                  0.7068    2.0275   1.1711    0.5871  1.20
susinidum_mar                  0.0357    1.0363   1.1510    0.5916  0.06
psycom_dum_with               -0.0400    0.9608   0.2528    0.2598 -0.15
psycom_dum_study               0.3892    1.4758   0.3255    0.3183  1.22
freq_cons_dum_5day             0.4022    1.4951   0.2703    0.2447  1.64
cond_oc_dum_2inact             0.3064    1.3585   0.3531    0.3455  0.89
cond_oc_dum_3unemp             0.1412    1.1517   0.3483    0.3110  0.45
susprindum_oh                  0.7067    2.0274   0.8058    0.6043  1.17
susprindum_coc                 0.6402    1.8969   0.8214    0.6175  1.04
susprindum_pbc                 0.6564    1.9279   0.7631    0.5779  1.14
susprindum_mar                 1.1354    3.1123   0.8929    0.6353  1.79
                              Pr(>|z|)    
tr_outcome.lag                   0.224    
less_90d_tr1.lag                 0.551    
log_dias_treat_imp_sin_na.lag    0.726    
comp_bpsc_y3_severe.lag          0.405    
policonsumo2.lag                 0.029 *  
edad_al_ing_1                  3.6e-09 ***
ano_nac_corr                   7.1e-09 ***
susinidum_oh                     0.682    
susinidum_coc                    0.013 *  
susinidum_pbc                    0.229    
susinidum_mar                    0.952    
psycom_dum_with                  0.878    
psycom_dum_study                 0.221    
freq_cons_dum_5day               0.100    
cond_oc_dum_2inact               0.375    
cond_oc_dum_3unemp               0.650    
susprindum_oh                    0.242    
susprindum_coc                   0.300    
susprindum_pbc                   0.256    
susprindum_mar                   0.074 .  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    2.005      0.499    0.6533     6.152
less_90d_tr1.lag                  1.650      0.606    0.3188     8.535
log_dias_treat_imp_sin_na.lag     0.954      1.048    0.7329     1.242
comp_bpsc_y3_severe.lag           0.634      1.578    0.2170     1.852
policonsumo2.lag                  0.359      2.786    0.1435     0.898
edad_al_ing_1                     2.064      0.485    1.6223     2.626
ano_nac_corr                      2.016      0.496    1.5901     2.556
susinidum_oh                      1.217      0.822    0.4755     3.113
susinidum_coc                     0.248      4.025    0.0832     0.742
susinidum_pbc                     2.028      0.493    0.6416     6.408
susinidum_mar                     1.036      0.965    0.3250     3.304
psycom_dum_with                   0.961      1.041    0.5774     1.599
psycom_dum_study                  1.476      0.678    0.7909     2.754
freq_cons_dum_5day                1.495      0.669    0.9255     2.415
cond_oc_dum_2inact                1.358      0.736    0.6901     2.674
cond_oc_dum_3unemp                1.152      0.868    0.6260     2.119
susprindum_oh                     2.027      0.493    0.6202     6.627
susprindum_coc                    1.897      0.527    0.5654     6.363
susprindum_pbc                    1.928      0.519    0.6211     5.985
susprindum_mar                    3.112      0.321    0.8961    10.810

Concordance= 0.691  (se = 0.024 )
Likelihood ratio test= 51.4  on 20 df,   p=1e-04
Wald test            = 286  on 20 df,   p=<2e-16
Score (logrank) test = 49.1  on 20 df,   p=3e-04,   Robust = 44.4  p=0.001

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.01    0.16    0.30    0.55    0.58    9.39 
Code
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:
iiws_strat_alt_alt <- list()

comb_strata_alt_alt <- expand.grid(unique(data_mine_miss_restr_proc2$tipo_de_plan_2_mod), 
                           unique(data_mine_miss_restr_proc2$time_interval3_alt))

for (ff in seq_along(1:nrow(comb_strata_alt_alt))) {
  i<- comb_strata_alt_alt[ff,"Var1"]
  j<- comb_strata_alt_alt[ff,"Var2"]
  data_mine_miss_proc2_subset<-
  subset(data_mine_miss_restr_proc2, subset= tipo_de_plan_2_mod==i & time_interval3_alt==j)
  maxfu_df_subset <- subset(maxfu_restr_df, subset= maxfu.id %in% unique(data_mine_miss_proc2_subset$id))  
  iiw_model_strat_alt_alt<-
    IrregLong::iiw.weights(Surv(time.lag,time,event)~ 
          cluster(id)+ #If a frailty model is used, the cluster(id) term should appear before other covariates
          tr_outcome.lag+
          less_90d_tr1.lag+
          log_dias_treat_imp_sin_na.lag +
          comp_bpsc_y3_severe.lag+
          policonsumo2.lag + 
          edad_al_ing_1 + 
          ano_nac_corr + 
          susinidum_oh +
          susinidum_coc +
          susinidum_pbc +
          susinidum_mar +
          psycom_dum_with +
          psycom_dum_study + 
          freq_cons_dum_5day +
          cond_oc_dum_2inact +
          cond_oc_dum_3unemp +
          susprindum_oh +
          susprindum_coc +
          susprindum_pbc +
          susprindum_mar, 
      data= data_mine_miss_proc2_subset,
      id= "id",
      time= "time",
      event= "event", #character string indicating which column of the data indicates whether or not a visit occurred. If every row corresponds to a visit, then this column will consist entirely of ones
      maxfu= maxfu_df_subset,
      invariant= c("edad_al_ing_1", "ano_nac_corr", "susinidum_oh", "susinidum_coc", "susinidum_pbc", "susinidum_mar",  "psycom_dum_with", "psycom_dum_study_rec2", "freq_cons_dum_5day", "cond_oc_dum_3unemp", "cond_oc_dum_2inact", "susprindum_oh", "susprindum_coc", "susprindum_pbc", "susprindum_mar"),
      lagvars= c("time", "tr_outcome","log_dias_treat_imp_sin_na", "less_90d_tr1","comp_bpsc_y3_severe", "policonsumo2"),
      lagfirst= c(2.95082,1,4.499811,1,1,1),  #90/30.5 4.499811 es 90 días
      first= T
)
  
  iiws_strat_alt_alt[[ff]] <- list(
      model = iiw_model_strat_alt_alt,
      comb = paste0("Modality: ",i,"\nTime interval: ", j,"."),
      cox_iiw = summary(iiw_model_strat_alt_alt$m),
      iiw = summary(iiw_model_strat_alt_alt$iiw)
  )
  print(paste0("Modality: ",i,"\nTime interval: ", j))
  print(summary(iiw_model_strat_alt_alt$m))
  print(summary(iiw_model_strat_alt_alt$iiw))

}
[1] "Modality: WO residential\nTime interval: [0,10]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 613, number of events= 613 
   (1197 observations deleted due to missingness)

                                 coef exp(coef) se(coef) robust se     z
tr_outcome.lag                -0.0900    0.9140   0.2501    0.1810 -0.50
less_90d_tr1.lag               0.7064    2.0268   0.1946    0.1616  4.37
log_dias_treat_imp_sin_na.lag  0.0461    1.0472   0.0939    0.0916  0.50
comp_bpsc_y3_severe.lag        0.5370    1.7108   0.1989    0.1883  2.85
policonsumo2.lag              -0.0548    0.9467   0.2159    0.2080 -0.26
edad_al_ing_1                 -0.0295    0.9710   0.0169    0.0169 -1.74
ano_nac_corr                  -0.0248    0.9755   0.0162    0.0164 -1.51
susinidum_oh                  -0.0586    0.9430   0.2951    0.2920 -0.20
susinidum_coc                 -0.3375    0.7136   0.3417    0.3291 -1.03
susinidum_pbc                  0.2985    1.3479   0.3018    0.3066  0.97
susinidum_mar                  0.1756    1.1920   0.2911    0.2907  0.60
psycom_dum_with               -0.2274    0.7966   0.1182    0.1159 -1.96
psycom_dum_study              -0.2578    0.7728   0.1608    0.1667 -1.55
freq_cons_dum_5day             0.0151    1.0152   0.0988    0.0946  0.16
cond_oc_dum_2inact             0.2079    1.2311   0.1547    0.1475  1.41
cond_oc_dum_3unemp             0.1580    1.1712   0.1547    0.1502  1.05
susprindum_oh                  0.3181    1.3746   0.2850    0.2514  1.27
susprindum_coc                 0.4414    1.5549   0.2868    0.2377  1.86
susprindum_pbc                 0.3105    1.3640   0.2662    0.2213  1.40
susprindum_mar                 0.4231    1.5267   0.4242    0.3693  1.15
                              Pr(>|z|)    
tr_outcome.lag                  0.6192    
less_90d_tr1.lag               1.2e-05 ***
log_dias_treat_imp_sin_na.lag   0.6147    
comp_bpsc_y3_severe.lag         0.0044 ** 
policonsumo2.lag                0.7921    
edad_al_ing_1                   0.0815 .  
ano_nac_corr                    0.1308    
susinidum_oh                    0.8409    
susinidum_coc                   0.3052    
susinidum_pbc                   0.3302    
susinidum_mar                   0.5457    
psycom_dum_with                 0.0497 *  
psycom_dum_study                0.1219    
freq_cons_dum_5day              0.8730    
cond_oc_dum_2inact              0.1588    
cond_oc_dum_3unemp              0.2928    
susprindum_oh                   0.2057    
susprindum_coc                  0.0633 .  
susprindum_pbc                  0.1607    
susprindum_mar                  0.2519    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    0.914      1.094     0.641      1.30
less_90d_tr1.lag                  2.027      0.493     1.477      2.78
log_dias_treat_imp_sin_na.lag     1.047      0.955     0.875      1.25
comp_bpsc_y3_severe.lag           1.711      0.585     1.183      2.47
policonsumo2.lag                  0.947      1.056     0.630      1.42
edad_al_ing_1                     0.971      1.030     0.939      1.00
ano_nac_corr                      0.976      1.025     0.945      1.01
susinidum_oh                      0.943      1.060     0.532      1.67
susinidum_coc                     0.714      1.401     0.374      1.36
susinidum_pbc                     1.348      0.742     0.739      2.46
susinidum_mar                     1.192      0.839     0.674      2.11
psycom_dum_with                   0.797      1.255     0.635      1.00
psycom_dum_study                  0.773      1.294     0.557      1.07
freq_cons_dum_5day                1.015      0.985     0.843      1.22
cond_oc_dum_2inact                1.231      0.812     0.922      1.64
cond_oc_dum_3unemp                1.171      0.854     0.873      1.57
susprindum_oh                     1.375      0.727     0.840      2.25
susprindum_coc                    1.555      0.643     0.976      2.48
susprindum_pbc                    1.364      0.733     0.884      2.10
susprindum_mar                    1.527      0.655     0.740      3.15

Concordance= 0.603  (se = 0.012 )
Likelihood ratio test= 62.1  on 20 df,   p=3e-06
Wald test            = 79.2  on 20 df,   p=5e-09
Score (logrank) test = 57.3  on 20 df,   p=2e-05,   Robust = 63.2  p=2e-06

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.125   0.197   0.226   0.251   0.280   1.222 
[1] "Modality: GP intensive ambulatory\nTime interval: [0,10]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 3052, number of events= 3052 
   (5099 observations deleted due to missingness)

                                  coef exp(coef) se(coef) robust se     z
tr_outcome.lag                -0.04944   0.95177  0.19147   0.14628 -0.34
less_90d_tr1.lag               0.37121   1.44949  0.09331   0.08001  4.64
log_dias_treat_imp_sin_na.lag  0.12986   1.13867  0.05504   0.05510  2.36
comp_bpsc_y3_severe.lag        0.58095   1.78773  0.07941   0.07147  8.13
policonsumo2.lag               0.09846   1.10347  0.12622   0.09638  1.02
edad_al_ing_1                  0.01296   1.01304  0.00803   0.00836  1.55
ano_nac_corr                   0.01642   1.01655  0.00788   0.00829  1.98
susinidum_oh                   0.04711   1.04824  0.13295   0.12352  0.38
susinidum_coc                  0.08476   1.08845  0.15803   0.14920  0.57
susinidum_pbc                  0.24050   1.27188  0.14782   0.14249  1.69
susinidum_mar                  0.09664   1.10146  0.13417   0.12551  0.77
psycom_dum_with               -0.06549   0.93661  0.03946   0.03958 -1.65
psycom_dum_study               0.30426   1.35562  0.06520   0.07476  4.07
freq_cons_dum_5day             0.03776   1.03848  0.03760   0.03824  0.99
cond_oc_dum_2inact            -0.00650   0.99353  0.05709   0.05742 -0.11
cond_oc_dum_3unemp             0.00356   1.00356  0.04041   0.04027  0.09
susprindum_oh                  0.08893   1.09301  0.17883   0.17307  0.51
susprindum_coc                 0.05586   1.05745  0.17922   0.17480  0.32
susprindum_pbc                 0.13574   1.14539  0.17488   0.17189  0.79
susprindum_mar                 0.07097   1.07355  0.18876   0.18724  0.38
                              Pr(>|z|)    
tr_outcome.lag                   0.735    
less_90d_tr1.lag               3.5e-06 ***
log_dias_treat_imp_sin_na.lag    0.018 *  
comp_bpsc_y3_severe.lag        4.4e-16 ***
policonsumo2.lag                 0.307    
edad_al_ing_1                    0.121    
ano_nac_corr                     0.048 *  
susinidum_oh                     0.703    
susinidum_coc                    0.570    
susinidum_pbc                    0.091 .  
susinidum_mar                    0.441    
psycom_dum_with                  0.098 .  
psycom_dum_study               4.7e-05 ***
freq_cons_dum_5day               0.323    
cond_oc_dum_2inact               0.910    
cond_oc_dum_3unemp               0.930    
susprindum_oh                    0.607    
susprindum_coc                   0.749    
susprindum_pbc                   0.430    
susprindum_mar                   0.705    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    0.952      1.051     0.715      1.27
less_90d_tr1.lag                  1.449      0.690     1.239      1.70
log_dias_treat_imp_sin_na.lag     1.139      0.878     1.022      1.27
comp_bpsc_y3_severe.lag           1.788      0.559     1.554      2.06
policonsumo2.lag                  1.103      0.906     0.914      1.33
edad_al_ing_1                     1.013      0.987     0.997      1.03
ano_nac_corr                      1.017      0.984     1.000      1.03
susinidum_oh                      1.048      0.954     0.823      1.34
susinidum_coc                     1.088      0.919     0.812      1.46
susinidum_pbc                     1.272      0.786     0.962      1.68
susinidum_mar                     1.101      0.908     0.861      1.41
psycom_dum_with                   0.937      1.068     0.867      1.01
psycom_dum_study                  1.356      0.738     1.171      1.57
freq_cons_dum_5day                1.038      0.963     0.963      1.12
cond_oc_dum_2inact                0.994      1.007     0.888      1.11
cond_oc_dum_3unemp                1.004      0.996     0.927      1.09
susprindum_oh                     1.093      0.915     0.779      1.53
susprindum_coc                    1.057      0.946     0.751      1.49
susprindum_pbc                    1.145      0.873     0.818      1.60
susprindum_mar                    1.074      0.931     0.744      1.55

Concordance= 0.585  (se = 0.006 )
Likelihood ratio test= 156  on 20 df,   p=<2e-16
Wald test            = 154  on 20 df,   p=<2e-16
Score (logrank) test = 141  on 20 df,   p=<2e-16,   Robust = 139  p=<2e-16

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.164   0.255   0.304   0.308   0.331   1.985 
[1] "Modality: basic ambulatory\nTime interval: [0,10]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 2876, number of events= 2876 
   (4323 observations deleted due to missingness)

                                  coef exp(coef) se(coef) robust se     z
tr_outcome.lag                 0.05522   1.05677  0.25750   0.21420  0.26
less_90d_tr1.lag               0.30335   1.35439  0.11227   0.10746  2.82
log_dias_treat_imp_sin_na.lag  0.01684   1.01699  0.06516   0.05205  0.32
comp_bpsc_y3_severe.lag        0.70747   2.02885  0.08855   0.08420  8.40
policonsumo2.lag              -0.01761   0.98254  0.12635   0.10821 -0.16
edad_al_ing_1                  0.04075   1.04159  0.00875   0.00923  4.42
ano_nac_corr                   0.04753   1.04867  0.00853   0.00912  5.21
susinidum_oh                   0.17495   1.19119  0.14627   0.19430  0.90
susinidum_coc                  0.38052   1.46304  0.16633   0.21181  1.80
susinidum_pbc                  0.22510   1.25244  0.16317   0.20567  1.09
susinidum_mar                  0.26295   1.30076  0.14697   0.19576  1.34
psycom_dum_with               -0.05866   0.94303  0.04148   0.04207 -1.39
psycom_dum_study               0.46136   1.58624  0.05948   0.06699  6.89
freq_cons_dum_5day             0.05859   1.06034  0.04306   0.04442  1.32
cond_oc_dum_2inact             0.09190   1.09626  0.06201   0.06085  1.51
cond_oc_dum_3unemp             0.03188   1.03239  0.04548   0.04753  0.67
susprindum_oh                  0.48317   1.62121  0.22515   0.20340  2.38
susprindum_coc                 0.56258   1.75519  0.22597   0.20514  2.74
susprindum_pbc                 0.48472   1.62372  0.22336   0.20300  2.39
susprindum_mar                 0.44114   1.55448  0.23236   0.20939  2.11
                              Pr(>|z|)    
tr_outcome.lag                  0.7966    
less_90d_tr1.lag                0.0048 ** 
log_dias_treat_imp_sin_na.lag   0.7462    
comp_bpsc_y3_severe.lag        < 2e-16 ***
policonsumo2.lag                0.8707    
edad_al_ing_1                  1.0e-05 ***
ano_nac_corr                   1.8e-07 ***
susinidum_oh                    0.3679    
susinidum_coc                   0.0724 .  
susinidum_pbc                   0.2738    
susinidum_mar                   0.1792    
psycom_dum_with                 0.1632    
psycom_dum_study               5.7e-12 ***
freq_cons_dum_5day              0.1871    
cond_oc_dum_2inact              0.1310    
cond_oc_dum_3unemp              0.5024    
susprindum_oh                   0.0175 *  
susprindum_coc                  0.0061 ** 
susprindum_pbc                  0.0169 *  
susprindum_mar                  0.0351 *  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    1.057      0.946     0.694      1.61
less_90d_tr1.lag                  1.354      0.738     1.097      1.67
log_dias_treat_imp_sin_na.lag     1.017      0.983     0.918      1.13
comp_bpsc_y3_severe.lag           2.029      0.493     1.720      2.39
policonsumo2.lag                  0.983      1.018     0.795      1.21
edad_al_ing_1                     1.042      0.960     1.023      1.06
ano_nac_corr                      1.049      0.954     1.030      1.07
susinidum_oh                      1.191      0.839     0.814      1.74
susinidum_coc                     1.463      0.684     0.966      2.22
susinidum_pbc                     1.252      0.798     0.837      1.87
susinidum_mar                     1.301      0.769     0.886      1.91
psycom_dum_with                   0.943      1.060     0.868      1.02
psycom_dum_study                  1.586      0.630     1.391      1.81
freq_cons_dum_5day                1.060      0.943     0.972      1.16
cond_oc_dum_2inact                1.096      0.912     0.973      1.24
cond_oc_dum_3unemp                1.032      0.969     0.941      1.13
susprindum_oh                     1.621      0.617     1.088      2.42
susprindum_coc                    1.755      0.570     1.174      2.62
susprindum_pbc                    1.624      0.616     1.091      2.42
susprindum_mar                    1.554      0.643     1.031      2.34

Concordance= 0.598  (se = 0.006 )
Likelihood ratio test= 222  on 20 df,   p=<2e-16
Wald test            = 212  on 20 df,   p=<2e-16
Score (logrank) test = 208  on 20 df,   p=<2e-16,   Robust = 200  p=<2e-16

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.060   0.132   0.165   0.170   0.188   1.000 
[1] "Modality: GP residential\nTime interval: [0,10]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 1378, number of events= 1378 
   (2346 observations deleted due to missingness)

                                  coef exp(coef) se(coef) robust se     z
tr_outcome.lag                 0.15244   1.16468  0.22389   0.19303  0.79
less_90d_tr1.lag               0.33669   1.40030  0.12075   0.09077  3.71
log_dias_treat_imp_sin_na.lag  0.05576   1.05735  0.05762   0.06101  0.91
comp_bpsc_y3_severe.lag        0.23214   1.26129  0.14637   0.09912  2.34
policonsumo2.lag               0.00257   1.00258  0.17348   0.14029  0.02
edad_al_ing_1                  0.01254   1.01262  0.01203   0.01134  1.11
ano_nac_corr                   0.01722   1.01737  0.01179   0.01141  1.51
susinidum_oh                   0.32715   1.38701  0.21824   0.18145  1.80
susinidum_coc                  0.31906   1.37584  0.25651   0.22809  1.40
susinidum_pbc                  0.28383   1.32820  0.23008   0.19598  1.45
susinidum_mar                  0.36668   1.44294  0.21914   0.18275  2.01
psycom_dum_with               -0.08436   0.91910  0.06180   0.06083 -1.39
psycom_dum_study               0.05554   1.05712  0.09488   0.10204  0.54
freq_cons_dum_5day             0.14407   1.15497  0.06361   0.06306  2.28
cond_oc_dum_2inact             0.00969   1.00974  0.10245   0.10074  0.10
cond_oc_dum_3unemp            -0.01209   0.98799  0.08698   0.08737 -0.14
susprindum_oh                  0.22130   1.24769  0.29117   0.17490  1.27
susprindum_coc                 0.12158   1.12928  0.29393   0.18007  0.68
susprindum_pbc                 0.25572   1.29139  0.28565   0.16995  1.50
susprindum_mar                 0.10095   1.10622  0.33636   0.23222  0.43
                              Pr(>|z|)    
tr_outcome.lag                 0.42968    
less_90d_tr1.lag               0.00021 ***
log_dias_treat_imp_sin_na.lag  0.36073    
comp_bpsc_y3_severe.lag        0.01919 *  
policonsumo2.lag               0.98537    
edad_al_ing_1                  0.26872    
ano_nac_corr                   0.13110    
susinidum_oh                   0.07139 .  
susinidum_coc                  0.16185    
susinidum_pbc                  0.14755    
susinidum_mar                  0.04480 *  
psycom_dum_with                0.16553    
psycom_dum_study               0.58621    
freq_cons_dum_5day             0.02232 *  
cond_oc_dum_2inact             0.92334    
cond_oc_dum_3unemp             0.88998    
susprindum_oh                  0.20577    
susprindum_coc                 0.49955    
susprindum_pbc                 0.13240    
susprindum_mar                 0.66377    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    1.165      0.859     0.798      1.70
less_90d_tr1.lag                  1.400      0.714     1.172      1.67
log_dias_treat_imp_sin_na.lag     1.057      0.946     0.938      1.19
comp_bpsc_y3_severe.lag           1.261      0.793     1.039      1.53
policonsumo2.lag                  1.003      0.997     0.762      1.32
edad_al_ing_1                     1.013      0.988     0.990      1.04
ano_nac_corr                      1.017      0.983     0.995      1.04
susinidum_oh                      1.387      0.721     0.972      1.98
susinidum_coc                     1.376      0.727     0.880      2.15
susinidum_pbc                     1.328      0.753     0.905      1.95
susinidum_mar                     1.443      0.693     1.009      2.06
psycom_dum_with                   0.919      1.088     0.816      1.04
psycom_dum_study                  1.057      0.946     0.865      1.29
freq_cons_dum_5day                1.155      0.866     1.021      1.31
cond_oc_dum_2inact                1.010      0.990     0.829      1.23
cond_oc_dum_3unemp                0.988      1.012     0.832      1.17
susprindum_oh                     1.248      0.801     0.886      1.76
susprindum_coc                    1.129      0.886     0.793      1.61
susprindum_pbc                    1.291      0.774     0.926      1.80
susprindum_mar                    1.106      0.904     0.702      1.74

Concordance= 0.567  (se = 0.009 )
Likelihood ratio test= 40.4  on 20 df,   p=0.004
Wald test            = 49.6  on 20 df,   p=3e-04
Score (logrank) test = 38.5  on 20 df,   p=0.008,   Robust = 42.4  p=0.002

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.184   0.233   0.256   0.265   0.284   0.584 
[1] "Modality: WO intensive ambulatory\nTime interval: [0,10]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 439, number of events= 439 
   (710 observations deleted due to missingness)

                                 coef exp(coef) se(coef) robust se     z
tr_outcome.lag                -0.7998    0.4494   1.0298    0.2328 -3.44
less_90d_tr1.lag               0.8186    2.2672   0.2904    0.2384  3.43
log_dias_treat_imp_sin_na.lag  0.3638    1.4389   0.2272    0.1822  2.00
comp_bpsc_y3_severe.lag        0.1953    1.2157   0.2301    0.1749  1.12
policonsumo2.lag               0.6427    1.9016   0.3232    0.2527  2.54
edad_al_ing_1                  0.0258    1.0261   0.0237    0.0230  1.12
ano_nac_corr                   0.0237    1.0239   0.0235    0.0231  1.02
susinidum_oh                  -0.0563    0.9453   0.3381    0.4084 -0.14
susinidum_coc                 -0.1008    0.9042   0.3994    0.4504 -0.22
susinidum_pbc                 -0.2883    0.7496   0.3687    0.4356 -0.66
susinidum_mar                 -0.1510    0.8598   0.3476    0.4131 -0.37
psycom_dum_with                0.2137    1.2383   0.1094    0.1041  2.05
psycom_dum_study               0.6845    1.9827   0.1883    0.1839  3.72
freq_cons_dum_5day             0.0397    1.0405   0.1021    0.1033  0.38
cond_oc_dum_2inact             0.1214    1.1291   0.1298    0.1209  1.00
cond_oc_dum_3unemp             0.1248    1.1329   0.1386    0.1308  0.95
susprindum_oh                  0.3577    1.4300   0.3910    0.2730  1.31
susprindum_coc                 0.6496    1.9149   0.3979    0.2808  2.31
susprindum_pbc                 0.6304    1.8784   0.3826    0.2603  2.42
susprindum_mar                 1.1684    3.2168   0.4236    0.2973  3.93
                              Pr(>|z|)    
tr_outcome.lag                 0.00059 ***
less_90d_tr1.lag               0.00060 ***
log_dias_treat_imp_sin_na.lag  0.04584 *  
comp_bpsc_y3_severe.lag        0.26411    
policonsumo2.lag               0.01098 *  
edad_al_ing_1                  0.26148    
ano_nac_corr                   0.30580    
susinidum_oh                   0.89044    
susinidum_coc                  0.82298    
susinidum_pbc                  0.50811    
susinidum_mar                  0.71473    
psycom_dum_with                0.04000 *  
psycom_dum_study               0.00020 ***
freq_cons_dum_5day             0.70098    
cond_oc_dum_2inact             0.31526    
cond_oc_dum_3unemp             0.34005    
susprindum_oh                  0.19007    
susprindum_coc                 0.02069 *  
susprindum_pbc                 0.01542 *  
susprindum_mar                 8.5e-05 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    0.449      2.225     0.285     0.709
less_90d_tr1.lag                  2.267      0.441     1.421     3.617
log_dias_treat_imp_sin_na.lag     1.439      0.695     1.007     2.056
comp_bpsc_y3_severe.lag           1.216      0.823     0.863     1.713
policonsumo2.lag                  1.902      0.526     1.159     3.121
edad_al_ing_1                     1.026      0.975     0.981     1.073
ano_nac_corr                      1.024      0.977     0.979     1.071
susinidum_oh                      0.945      1.058     0.425     2.105
susinidum_coc                     0.904      1.106     0.374     2.186
susinidum_pbc                     0.750      1.334     0.319     1.760
susinidum_mar                     0.860      1.163     0.383     1.932
psycom_dum_with                   1.238      0.808     1.010     1.519
psycom_dum_study                  1.983      0.504     1.383     2.843
freq_cons_dum_5day                1.040      0.961     0.850     1.274
cond_oc_dum_2inact                1.129      0.886     0.891     1.431
cond_oc_dum_3unemp                1.133      0.883     0.877     1.464
susprindum_oh                     1.430      0.699     0.838     2.442
susprindum_coc                    1.915      0.522     1.104     3.320
susprindum_pbc                    1.878      0.532     1.128     3.128
susprindum_mar                    3.217      0.311     1.796     5.761

Concordance= 0.612  (se = 0.015 )
Likelihood ratio test= 42.4  on 20 df,   p=0.002
Wald test            = 83.1  on 20 df,   p=1e-09
Score (logrank) test = 39.6  on 20 df,   p=0.006,   Robust = 52.3  p=1e-04

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.059   0.145   0.190   0.201   0.230   1.315 
[1] "Modality: WO residential\nTime interval: (10,20]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 510, number of events= 510 
   (460 observations deleted due to missingness)

                                  coef exp(coef) se(coef) robust se     z
tr_outcome.lag                 0.21592   1.24101  0.34608   0.31001  0.70
less_90d_tr1.lag               0.89083   2.43714  0.41911   0.35694  2.50
log_dias_treat_imp_sin_na.lag  0.34591   1.41328  0.31806   0.22707  1.52
comp_bpsc_y3_severe.lag       -0.11404   0.89222  0.34659   0.28279 -0.40
policonsumo2.lag              -0.09157   0.91250  0.45600   0.21426 -0.43
edad_al_ing_1                 -0.01132   0.98874  0.01971   0.01840 -0.62
ano_nac_corr                  -0.01574   0.98439  0.01894   0.01751 -0.90
susinidum_oh                  -0.30205   0.73930  0.29743   0.42830 -0.71
susinidum_coc                 -0.26431   0.76773  0.34479   0.45702 -0.58
susinidum_pbc                 -0.51114   0.59981  0.30812   0.44117 -1.16
susinidum_mar                 -0.31219   0.73184  0.29812   0.42846 -0.73
psycom_dum_with                0.09827   1.10326  0.13967   0.13716  0.72
psycom_dum_study               0.06058   1.06245  0.16328   0.16532  0.37
freq_cons_dum_5day             0.04197   1.04286  0.10563   0.10603  0.40
cond_oc_dum_2inact             0.09249   1.09690  0.16558   0.15013  0.62
cond_oc_dum_3unemp            -0.04958   0.95162  0.16511   0.15264 -0.32
susprindum_oh                  0.13041   1.13929  0.29484   0.29199  0.45
susprindum_coc                -0.31573   0.72926  0.30716   0.31845 -0.99
susprindum_pbc                 0.00569   1.00571  0.28545   0.28959  0.02
susprindum_mar                -0.87667   0.41617  0.53610   0.39618 -2.21
                              Pr(>|z|)  
tr_outcome.lag                   0.486  
less_90d_tr1.lag                 0.013 *
log_dias_treat_imp_sin_na.lag    0.128  
comp_bpsc_y3_severe.lag          0.687  
policonsumo2.lag                 0.669  
edad_al_ing_1                    0.538  
ano_nac_corr                     0.369  
susinidum_oh                     0.481  
susinidum_coc                    0.563  
susinidum_pbc                    0.247  
susinidum_mar                    0.466  
psycom_dum_with                  0.474  
psycom_dum_study                 0.714  
freq_cons_dum_5day               0.692  
cond_oc_dum_2inact               0.538  
cond_oc_dum_3unemp               0.745  
susprindum_oh                    0.655  
susprindum_coc                   0.321  
susprindum_pbc                   0.984  
susprindum_mar                   0.027 *
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    1.241      0.806     0.676     2.279
less_90d_tr1.lag                  2.437      0.410     1.211     4.906
log_dias_treat_imp_sin_na.lag     1.413      0.708     0.906     2.205
comp_bpsc_y3_severe.lag           0.892      1.121     0.513     1.553
policonsumo2.lag                  0.913      1.096     0.600     1.389
edad_al_ing_1                     0.989      1.011     0.954     1.025
ano_nac_corr                      0.984      1.016     0.951     1.019
susinidum_oh                      0.739      1.353     0.319     1.712
susinidum_coc                     0.768      1.303     0.313     1.880
susinidum_pbc                     0.600      1.667     0.253     1.424
susinidum_mar                     0.732      1.366     0.316     1.695
psycom_dum_with                   1.103      0.906     0.843     1.444
psycom_dum_study                  1.062      0.941     0.768     1.469
freq_cons_dum_5day                1.043      0.959     0.847     1.284
cond_oc_dum_2inact                1.097      0.912     0.817     1.472
cond_oc_dum_3unemp                0.952      1.051     0.706     1.283
susprindum_oh                     1.139      0.878     0.643     2.019
susprindum_coc                    0.729      1.371     0.391     1.361
susprindum_pbc                    1.006      0.994     0.570     1.774
susprindum_mar                    0.416      2.403     0.191     0.905

Concordance= 0.563  (se = 0.014 )
Likelihood ratio test= 31.6  on 20 df,   p=0.05
Wald test            = 48.2  on 20 df,   p=4e-04
Score (logrank) test = 29.6  on 20 df,   p=0.08,   Robust = 36.9  p=0.01

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.318   0.466   0.529   0.571   0.630   1.853 
[1] "Modality: GP intensive ambulatory\nTime interval: (10,20]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 2228, number of events= 2228 
   (2101 observations deleted due to missingness)

                                  coef exp(coef) se(coef) robust se     z
tr_outcome.lag                -0.21836   0.80384  0.20606   0.15412 -1.42
less_90d_tr1.lag               0.75859   2.13527  0.26008   0.22146  3.43
log_dias_treat_imp_sin_na.lag  0.01313   1.01322  0.14279   0.12222  0.11
comp_bpsc_y3_severe.lag        0.10611   1.11194  0.17834   0.12291  0.86
policonsumo2.lag               0.02144   1.02167  0.22376   0.14550  0.15
edad_al_ing_1                 -0.00175   0.99826  0.00887   0.00879 -0.20
ano_nac_corr                  -0.00275   0.99726  0.00859   0.00856 -0.32
susinidum_oh                   0.05307   1.05451  0.15536   0.17220  0.31
susinidum_coc                  0.15066   1.16261  0.18055   0.18971  0.79
susinidum_pbc                  0.06123   1.06314  0.17256   0.19045  0.32
susinidum_mar                  0.03864   1.03939  0.15764   0.17564  0.22
psycom_dum_with               -0.02080   0.97941  0.04721   0.04682 -0.44
psycom_dum_study              -0.06091   0.94091  0.06572   0.06445 -0.95
freq_cons_dum_5day            -0.01579   0.98433  0.04386   0.04367 -0.36
cond_oc_dum_2inact             0.06525   1.06743  0.06486   0.06484  1.01
cond_oc_dum_3unemp             0.07212   1.07479  0.04734   0.04719  1.53
susprindum_oh                  0.08099   1.08436  0.17644   0.18468  0.44
susprindum_coc                 0.04766   1.04881  0.17743   0.18575  0.26
susprindum_pbc                 0.07136   1.07397  0.17324   0.18145  0.39
susprindum_mar                 0.13351   1.14283  0.19634   0.20052  0.67
                              Pr(>|z|)    
tr_outcome.lag                 0.15655    
less_90d_tr1.lag               0.00061 ***
log_dias_treat_imp_sin_na.lag  0.91444    
comp_bpsc_y3_severe.lag        0.38798    
policonsumo2.lag               0.88285    
edad_al_ing_1                  0.84261    
ano_nac_corr                   0.74832    
susinidum_oh                   0.75792    
susinidum_coc                  0.42708    
susinidum_pbc                  0.74785    
susinidum_mar                  0.82589    
psycom_dum_with                0.65677    
psycom_dum_study               0.34465    
freq_cons_dum_5day             0.71767    
cond_oc_dum_2inact             0.31424    
cond_oc_dum_3unemp             0.12643    
susprindum_oh                  0.66098    
susprindum_coc                 0.79751    
susprindum_pbc                 0.69410    
susprindum_mar                 0.50555    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    0.804      1.244     0.594      1.09
less_90d_tr1.lag                  2.135      0.468     1.383      3.30
log_dias_treat_imp_sin_na.lag     1.013      0.987     0.797      1.29
comp_bpsc_y3_severe.lag           1.112      0.899     0.874      1.41
policonsumo2.lag                  1.022      0.979     0.768      1.36
edad_al_ing_1                     0.998      1.002     0.981      1.02
ano_nac_corr                      0.997      1.003     0.981      1.01
susinidum_oh                      1.055      0.948     0.752      1.48
susinidum_coc                     1.163      0.860     0.802      1.69
susinidum_pbc                     1.063      0.941     0.732      1.54
susinidum_mar                     1.039      0.962     0.737      1.47
psycom_dum_with                   0.979      1.021     0.894      1.07
psycom_dum_study                  0.941      1.063     0.829      1.07
freq_cons_dum_5day                0.984      1.016     0.904      1.07
cond_oc_dum_2inact                1.067      0.937     0.940      1.21
cond_oc_dum_3unemp                1.075      0.930     0.980      1.18
susprindum_oh                     1.084      0.922     0.755      1.56
susprindum_coc                    1.049      0.953     0.729      1.51
susprindum_pbc                    1.074      0.931     0.753      1.53
susprindum_mar                    1.143      0.875     0.771      1.69

Concordance= 0.528  (se = 0.007 )
Likelihood ratio test= 67.3  on 20 df,   p=5e-07
Wald test            = 93.3  on 20 df,   p=2e-11
Score (logrank) test = 57.5  on 20 df,   p=2e-05,   Robust = 47.3  p=5e-04

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.365   0.433   0.450   0.471   0.466   1.154 
[1] "Modality: basic ambulatory\nTime interval: (10,20]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 1787, number of events= 1787 
   (1705 observations deleted due to missingness)

                                  coef exp(coef) se(coef) robust se     z
tr_outcome.lag                -0.11289   0.89325  0.26499   0.19738 -0.57
less_90d_tr1.lag               0.30966   1.36296  0.42422   0.33394  0.93
log_dias_treat_imp_sin_na.lag -0.03569   0.96494  0.22487   0.17960 -0.20
comp_bpsc_y3_severe.lag       -0.01927   0.98091  0.27409   0.21820 -0.09
policonsumo2.lag               0.30523   1.35693  0.23823   0.21449  1.42
edad_al_ing_1                 -0.00444   0.99557  0.01000   0.00997 -0.45
ano_nac_corr                  -0.00873   0.99130  0.00971   0.00979 -0.89
susinidum_oh                  -0.10940   0.89637  0.17579   0.19112 -0.57
susinidum_coc                 -0.25157   0.77758  0.20960   0.21688 -1.16
susinidum_pbc                 -0.03937   0.96140  0.19588   0.21149 -0.19
susinidum_mar                 -0.07741   0.92551  0.17892   0.19344 -0.40
psycom_dum_with               -0.05845   0.94322  0.05253   0.05173 -1.13
psycom_dum_study              -0.14794   0.86249  0.07226   0.07361 -2.01
freq_cons_dum_5day             0.02945   1.02989  0.05516   0.05564  0.53
cond_oc_dum_2inact            -0.02692   0.97344  0.07140   0.07258 -0.37
cond_oc_dum_3unemp            -0.02013   0.98007  0.05935   0.05786 -0.35
susprindum_oh                  0.04298   1.04392  0.20176   0.16993  0.25
susprindum_coc                 0.05952   1.06133  0.20562   0.17324  0.34
susprindum_pbc                -0.01571   0.98442  0.20156   0.17115 -0.09
susprindum_mar                 0.22047   1.24666  0.21393   0.18470  1.19
                              Pr(>|z|)  
tr_outcome.lag                   0.567  
less_90d_tr1.lag                 0.354  
log_dias_treat_imp_sin_na.lag    0.842  
comp_bpsc_y3_severe.lag          0.930  
policonsumo2.lag                 0.155  
edad_al_ing_1                    0.656  
ano_nac_corr                     0.372  
susinidum_oh                     0.567  
susinidum_coc                    0.246  
susinidum_pbc                    0.852  
susinidum_mar                    0.689  
psycom_dum_with                  0.258  
psycom_dum_study                 0.044 *
freq_cons_dum_5day               0.597  
cond_oc_dum_2inact               0.711  
cond_oc_dum_3unemp               0.728  
susprindum_oh                    0.800  
susprindum_coc                   0.731  
susprindum_pbc                   0.927  
susprindum_mar                   0.233  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    0.893      1.120     0.607     1.315
less_90d_tr1.lag                  1.363      0.734     0.708     2.623
log_dias_treat_imp_sin_na.lag     0.965      1.036     0.679     1.372
comp_bpsc_y3_severe.lag           0.981      1.019     0.640     1.504
policonsumo2.lag                  1.357      0.737     0.891     2.066
edad_al_ing_1                     0.996      1.004     0.976     1.015
ano_nac_corr                      0.991      1.009     0.972     1.011
susinidum_oh                      0.896      1.116     0.616     1.304
susinidum_coc                     0.778      1.286     0.508     1.189
susinidum_pbc                     0.961      1.040     0.635     1.455
susinidum_mar                     0.926      1.080     0.633     1.352
psycom_dum_with                   0.943      1.060     0.852     1.044
psycom_dum_study                  0.862      1.159     0.747     0.996
freq_cons_dum_5day                1.030      0.971     0.923     1.149
cond_oc_dum_2inact                0.973      1.027     0.844     1.122
cond_oc_dum_3unemp                0.980      1.020     0.875     1.098
susprindum_oh                     1.044      0.958     0.748     1.456
susprindum_coc                    1.061      0.942     0.756     1.490
susprindum_pbc                    0.984      1.016     0.704     1.377
susprindum_mar                    1.247      0.802     0.868     1.790

Concordance= 0.535  (se = 0.008 )
Likelihood ratio test= 28.9  on 20 df,   p=0.09
Wald test            = 34.3  on 20 df,   p=0.02
Score (logrank) test = 27.8  on 20 df,   p=0.1,   Robust = 29.2  p=0.08

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.473   0.647   0.688   0.701   0.737   1.481 
[1] "Modality: GP residential\nTime interval: (10,20]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 1104, number of events= 1104 
   (1012 observations deleted due to missingness)

                                  coef exp(coef) se(coef) robust se     z
tr_outcome.lag                 0.20849   1.23182  0.22524   0.16977  1.23
less_90d_tr1.lag               0.18363   1.20157  0.28968   0.20840  0.88
log_dias_treat_imp_sin_na.lag -0.15939   0.85267  0.15317   0.09437 -1.69
comp_bpsc_y3_severe.lag        0.41048   1.50754  0.24543   0.19143  2.14
policonsumo2.lag               0.32097   1.37847  0.35877   0.20629  1.56
edad_al_ing_1                  0.00535   1.00537  0.01386   0.01396  0.38
ano_nac_corr                   0.00559   1.00561  0.01396   0.01365  0.41
susinidum_oh                  -0.15489   0.85651  0.21234   0.18000 -0.86
susinidum_coc                 -0.02269   0.97757  0.26191   0.22529 -0.10
susinidum_pbc                 -0.29053   0.74786  0.22752   0.19736 -1.47
susinidum_mar                 -0.14415   0.86575  0.21119   0.17822 -0.81
psycom_dum_with               -0.07780   0.92515  0.07047   0.06905 -1.13
psycom_dum_study              -0.18985   0.82708  0.09881   0.09247 -2.05
freq_cons_dum_5day            -0.02728   0.97309  0.06870   0.06836 -0.40
cond_oc_dum_2inact            -0.00107   0.99893  0.11269   0.11224 -0.01
cond_oc_dum_3unemp             0.11521   1.12211  0.09200   0.08840  1.30
susprindum_oh                 -0.04029   0.96051  0.31288   0.27695 -0.15
susprindum_coc                -0.16429   0.84849  0.31546   0.28015 -0.59
susprindum_pbc                -0.15457   0.85678  0.30654   0.27261 -0.57
susprindum_mar                 0.41564   1.51533  0.35411   0.32712  1.27
                              Pr(>|z|)  
tr_outcome.lag                   0.219  
less_90d_tr1.lag                 0.378  
log_dias_treat_imp_sin_na.lag    0.091 .
comp_bpsc_y3_severe.lag          0.032 *
policonsumo2.lag                 0.120  
edad_al_ing_1                    0.701  
ano_nac_corr                     0.682  
susinidum_oh                     0.390  
susinidum_coc                    0.920  
susinidum_pbc                    0.141  
susinidum_mar                    0.419  
psycom_dum_with                  0.260  
psycom_dum_study                 0.040 *
freq_cons_dum_5day               0.690  
cond_oc_dum_2inact               0.992  
cond_oc_dum_3unemp               0.192  
susprindum_oh                    0.884  
susprindum_coc                   0.558  
susprindum_pbc                   0.571  
susprindum_mar                   0.204  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    1.232      0.812     0.883     1.718
less_90d_tr1.lag                  1.202      0.832     0.799     1.808
log_dias_treat_imp_sin_na.lag     0.853      1.173     0.709     1.026
comp_bpsc_y3_severe.lag           1.508      0.663     1.036     2.194
policonsumo2.lag                  1.378      0.725     0.920     2.065
edad_al_ing_1                     1.005      0.995     0.978     1.033
ano_nac_corr                      1.006      0.994     0.979     1.033
susinidum_oh                      0.857      1.168     0.602     1.219
susinidum_coc                     0.978      1.023     0.629     1.520
susinidum_pbc                     0.748      1.337     0.508     1.101
susinidum_mar                     0.866      1.155     0.611     1.228
psycom_dum_with                   0.925      1.081     0.808     1.059
psycom_dum_study                  0.827      1.209     0.690     0.991
freq_cons_dum_5day                0.973      1.028     0.851     1.113
cond_oc_dum_2inact                0.999      1.001     0.802     1.245
cond_oc_dum_3unemp                1.122      0.891     0.944     1.334
susprindum_oh                     0.961      1.041     0.558     1.653
susprindum_coc                    0.848      1.179     0.490     1.469
susprindum_pbc                    0.857      1.167     0.502     1.462
susprindum_mar                    1.515      0.660     0.798     2.877

Concordance= 0.549  (se = 0.01 )
Likelihood ratio test= 50.8  on 20 df,   p=2e-04
Wald test            = 69.5  on 20 df,   p=2e-07
Score (logrank) test = 47.6  on 20 df,   p=5e-04,   Robust = 46.6  p=7e-04

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.194   0.403   0.432   0.459   0.478   1.500 
[1] "Modality: WO intensive ambulatory\nTime interval: (10,20]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 383, number of events= 383 
   (363 observations deleted due to missingness)

                                 coef exp(coef) se(coef) robust se     z
tr_outcome.lag                 0.0634    1.0654   0.5857    0.2750  0.23
less_90d_tr1.lag               0.1451    1.1562   0.6219    0.2496  0.58
log_dias_treat_imp_sin_na.lag -0.0925    0.9116   0.4925    0.2183 -0.42
comp_bpsc_y3_severe.lag        0.2509    1.2852   0.4928    0.2624  0.96
policonsumo2.lag               0.1765    1.1931   0.5414    0.2508  0.70
edad_al_ing_1                  0.0183    1.0185   0.0229    0.0233  0.79
ano_nac_corr                   0.0113    1.0113   0.0226    0.0217  0.52
susinidum_oh                  -0.2012    0.8178   0.3296    0.2297 -0.88
susinidum_coc                 -0.2162    0.8055   0.3882    0.2877 -0.75
susinidum_pbc                 -0.3864    0.6795   0.3519    0.2679 -1.44
susinidum_mar                 -0.2634    0.7684   0.3302    0.2266 -1.16
psycom_dum_with               -0.0683    0.9340   0.1235    0.1174 -0.58
psycom_dum_study              -0.0856    0.9179   0.1662    0.1586 -0.54
freq_cons_dum_5day             0.0226    1.0229   0.1079    0.1064  0.21
cond_oc_dum_2inact            -0.2520    0.7773   0.1403    0.1450 -1.74
cond_oc_dum_3unemp            -0.1483    0.8621   0.1438    0.1474 -1.01
susprindum_oh                 -0.1126    0.8935   0.4137    0.3334 -0.34
susprindum_coc                 0.1187    1.1260   0.4111    0.3188  0.37
susprindum_pbc                 0.0363    1.0370   0.4006    0.3042  0.12
susprindum_mar                 0.3433    1.4095   0.4282    0.3499  0.98
                              Pr(>|z|)  
tr_outcome.lag                   0.818  
less_90d_tr1.lag                 0.561  
log_dias_treat_imp_sin_na.lag    0.672  
comp_bpsc_y3_severe.lag          0.339  
policonsumo2.lag                 0.482  
edad_al_ing_1                    0.431  
ano_nac_corr                     0.604  
susinidum_oh                     0.381  
susinidum_coc                    0.452  
susinidum_pbc                    0.149  
susinidum_mar                    0.245  
psycom_dum_with                  0.561  
psycom_dum_study                 0.589  
freq_cons_dum_5day               0.832  
cond_oc_dum_2inact               0.082 .
cond_oc_dum_3unemp               0.314  
susprindum_oh                    0.736  
susprindum_coc                   0.710  
susprindum_pbc                   0.905  
susprindum_mar                   0.327  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    1.065      0.939     0.622      1.83
less_90d_tr1.lag                  1.156      0.865     0.709      1.89
log_dias_treat_imp_sin_na.lag     0.912      1.097     0.594      1.40
comp_bpsc_y3_severe.lag           1.285      0.778     0.768      2.15
policonsumo2.lag                  1.193      0.838     0.730      1.95
edad_al_ing_1                     1.018      0.982     0.973      1.07
ano_nac_corr                      1.011      0.989     0.969      1.06
susinidum_oh                      0.818      1.223     0.521      1.28
susinidum_coc                     0.806      1.241     0.458      1.42
susinidum_pbc                     0.680      1.472     0.402      1.15
susinidum_mar                     0.768      1.301     0.493      1.20
psycom_dum_with                   0.934      1.071     0.742      1.18
psycom_dum_study                  0.918      1.089     0.673      1.25
freq_cons_dum_5day                1.023      0.978     0.830      1.26
cond_oc_dum_2inact                0.777      1.287     0.585      1.03
cond_oc_dum_3unemp                0.862      1.160     0.646      1.15
susprindum_oh                     0.894      1.119     0.465      1.72
susprindum_coc                    1.126      0.888     0.603      2.10
susprindum_pbc                    1.037      0.964     0.571      1.88
susprindum_mar                    1.410      0.709     0.710      2.80

Concordance= 0.558  (se = 0.016 )
Likelihood ratio test= 14.6  on 20 df,   p=0.8
Wald test            = 22.8  on 20 df,   p=0.3
Score (logrank) test = 14.9  on 20 df,   p=0.8,   Robust = 21.6  p=0.4

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.401   0.701   0.802   0.808   0.907   1.542 
[1] "Modality: WO residential\nTime interval: (20,30]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 337, number of events= 337 
   (312 observations deleted due to missingness)

                                  coef exp(coef) se(coef) robust se     z
tr_outcome.lag                -0.54805   0.57808  0.63302   0.56170 -0.98
less_90d_tr1.lag               1.55245   4.72302  0.44287   0.48428  3.21
log_dias_treat_imp_sin_na.lag  0.44260   1.55674  0.23419   0.18393  2.41
comp_bpsc_y3_severe.lag       -0.33788   0.71328  0.54061   0.46918 -0.72
policonsumo2.lag              -1.38744   0.24971  0.65732   0.48749 -2.85
edad_al_ing_1                  0.00518   1.00520  0.02817   0.02923  0.18
ano_nac_corr                   0.01173   1.01180  0.02745   0.02832  0.41
susinidum_oh                  -0.89518   0.40854  0.36761   0.34203 -2.62
susinidum_coc                 -0.42191   0.65579  0.44454   0.46681 -0.90
susinidum_pbc                 -0.80592   0.44668  0.38239   0.35110 -2.30
susinidum_mar                 -0.67735   0.50796  0.37139   0.34057 -1.99
psycom_dum_with                0.11314   1.11979  0.16892   0.16214  0.70
psycom_dum_study               0.17197   1.18764  0.19648   0.18875  0.91
freq_cons_dum_5day            -0.12210   0.88506  0.13524   0.12778 -0.96
cond_oc_dum_2inact             0.01466   1.01477  0.21303   0.20392  0.07
cond_oc_dum_3unemp             0.11172   1.11820  0.21404   0.20170  0.55
susprindum_oh                  0.60878   1.83818  0.36597   0.33412  1.82
susprindum_coc                 0.20307   1.22516  0.36591   0.34566  0.59
susprindum_pbc                 0.55324   1.73888  0.34393   0.32150  1.72
susprindum_mar                -0.06475   0.93731  0.49574   0.46418 -0.14
                              Pr(>|z|)   
tr_outcome.lag                  0.3292   
less_90d_tr1.lag                0.0013 **
log_dias_treat_imp_sin_na.lag   0.0161 * 
comp_bpsc_y3_severe.lag         0.4714   
policonsumo2.lag                0.0044 **
edad_al_ing_1                   0.8593   
ano_nac_corr                    0.6788   
susinidum_oh                    0.0089 **
susinidum_coc                   0.3661   
susinidum_pbc                   0.0217 * 
susinidum_mar                   0.0467 * 
psycom_dum_with                 0.4853   
psycom_dum_study                0.3622   
freq_cons_dum_5day              0.3393   
cond_oc_dum_2inact              0.9427   
cond_oc_dum_3unemp              0.5796   
susprindum_oh                   0.0685 . 
susprindum_coc                  0.5569   
susprindum_pbc                  0.0853 . 
susprindum_mar                  0.8891   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    0.578      1.730     0.192     1.738
less_90d_tr1.lag                  4.723      0.212     1.828    12.202
log_dias_treat_imp_sin_na.lag     1.557      0.642     1.086     2.232
comp_bpsc_y3_severe.lag           0.713      1.402     0.284     1.789
policonsumo2.lag                  0.250      4.005     0.096     0.649
edad_al_ing_1                     1.005      0.995     0.949     1.064
ano_nac_corr                      1.012      0.988     0.957     1.070
susinidum_oh                      0.409      2.448     0.209     0.799
susinidum_coc                     0.656      1.525     0.263     1.637
susinidum_pbc                     0.447      2.239     0.224     0.889
susinidum_mar                     0.508      1.969     0.261     0.990
psycom_dum_with                   1.120      0.893     0.815     1.539
psycom_dum_study                  1.188      0.842     0.820     1.719
freq_cons_dum_5day                0.885      1.130     0.689     1.137
cond_oc_dum_2inact                1.015      0.985     0.680     1.513
cond_oc_dum_3unemp                1.118      0.894     0.753     1.660
susprindum_oh                     1.838      0.544     0.955     3.538
susprindum_coc                    1.225      0.816     0.622     2.412
susprindum_pbc                    1.739      0.575     0.926     3.265
susprindum_mar                    0.937      1.067     0.377     2.328

Concordance= 0.58  (se = 0.019 )
Likelihood ratio test= 33.7  on 20 df,   p=0.03
Wald test            = 30.1  on 20 df,   p=0.07
Score (logrank) test = 32.4  on 20 df,   p=0.04,   Robust = 29.8  p=0.07

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.94    2.20    2.54    2.79    2.96   11.34 
[1] "Modality: GP intensive ambulatory\nTime interval: (20,30]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 1468, number of events= 1468 
   (1416 observations deleted due to missingness)

                                  coef exp(coef) se(coef) robust se     z
tr_outcome.lag                -0.88258   0.41371  0.34605   0.23629 -3.74
less_90d_tr1.lag               0.69646   2.00664  0.30727   0.18314  3.80
log_dias_treat_imp_sin_na.lag -0.11098   0.89496  0.17582   0.11884 -0.93
comp_bpsc_y3_severe.lag        0.09737   1.10227  0.26731   0.19971  0.49
policonsumo2.lag              -0.16274   0.84981  0.37994   0.19389 -0.84
edad_al_ing_1                  0.00906   1.00911  0.01199   0.01156  0.78
ano_nac_corr                   0.01241   1.01249  0.01185   0.01152  1.08
susinidum_oh                  -0.17156   0.84235  0.17261   0.14429 -1.19
susinidum_coc                 -0.12936   0.87865  0.20917   0.18547 -0.70
susinidum_pbc                 -0.24760   0.78068  0.19361   0.16656 -1.49
susinidum_mar                 -0.14856   0.86195  0.17518   0.14781 -1.01
psycom_dum_with                0.07155   1.07417  0.05849   0.05756  1.24
psycom_dum_study               0.13765   1.14757  0.07960   0.08249  1.67
freq_cons_dum_5day            -0.02325   0.97702  0.05463   0.05461 -0.43
cond_oc_dum_2inact            -0.16820   0.84519  0.08031   0.07533 -2.23
cond_oc_dum_3unemp            -0.07700   0.92589  0.05856   0.05873 -1.31
susprindum_oh                  0.34467   1.41152  0.22480   0.22376  1.54
susprindum_coc                 0.37247   1.45131  0.22768   0.22788  1.63
susprindum_pbc                 0.36381   1.43880  0.21944   0.21983  1.65
susprindum_mar                 0.54116   1.71799  0.24309   0.24170  2.24
                              Pr(>|z|)    
tr_outcome.lag                 0.00019 ***
less_90d_tr1.lag               0.00014 ***
log_dias_treat_imp_sin_na.lag  0.35037    
comp_bpsc_y3_severe.lag        0.62586    
policonsumo2.lag               0.40130    
edad_al_ing_1                  0.43300    
ano_nac_corr                   0.28143    
susinidum_oh                   0.23445    
susinidum_coc                  0.48548    
susinidum_pbc                  0.13714    
susinidum_mar                  0.31485    
psycom_dum_with                0.21384    
psycom_dum_study               0.09516 .  
freq_cons_dum_5day             0.67023    
cond_oc_dum_2inact             0.02556 *  
cond_oc_dum_3unemp             0.18983    
susprindum_oh                  0.12347    
susprindum_coc                 0.10216    
susprindum_pbc                 0.09793 .  
susprindum_mar                 0.02516 *  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    0.414      2.417     0.260     0.657
less_90d_tr1.lag                  2.007      0.498     1.401     2.873
log_dias_treat_imp_sin_na.lag     0.895      1.117     0.709     1.130
comp_bpsc_y3_severe.lag           1.102      0.907     0.745     1.630
policonsumo2.lag                  0.850      1.177     0.581     1.243
edad_al_ing_1                     1.009      0.991     0.986     1.032
ano_nac_corr                      1.012      0.988     0.990     1.036
susinidum_oh                      0.842      1.187     0.635     1.118
susinidum_coc                     0.879      1.138     0.611     1.264
susinidum_pbc                     0.781      1.281     0.563     1.082
susinidum_mar                     0.862      1.160     0.645     1.152
psycom_dum_with                   1.074      0.931     0.960     1.202
psycom_dum_study                  1.148      0.871     0.976     1.349
freq_cons_dum_5day                0.977      1.024     0.878     1.087
cond_oc_dum_2inact                0.845      1.183     0.729     0.980
cond_oc_dum_3unemp                0.926      1.080     0.825     1.039
susprindum_oh                     1.412      0.708     0.910     2.189
susprindum_coc                    1.451      0.689     0.929     2.268
susprindum_pbc                    1.439      0.695     0.935     2.214
susprindum_mar                    1.718      0.582     1.070     2.759

Concordance= 0.536  (se = 0.009 )
Likelihood ratio test= 46.4  on 20 df,   p=7e-04
Wald test            = 54  on 20 df,   p=6e-05
Score (logrank) test = 41.5  on 20 df,   p=0.003,   Robust = 35.7  p=0.02

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.662   0.987   1.058   1.093   1.145   3.159 
[1] "Modality: basic ambulatory\nTime interval: (20,30]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 1208, number of events= 1208 
   (1172 observations deleted due to missingness)

                                 coef exp(coef) se(coef) robust se     z
tr_outcome.lag                 0.2222    1.2488   0.4500    0.3032  0.73
less_90d_tr1.lag               0.8375    2.3106   0.5200    0.4845  1.73
log_dias_treat_imp_sin_na.lag  0.4142    1.5132   0.3048    0.2076  1.99
comp_bpsc_y3_severe.lag        0.0768    1.0799   0.3167    0.3050  0.25
policonsumo2.lag               0.3782    1.4596   0.4335    0.3065  1.23
edad_al_ing_1                  0.0414    1.0422   0.0138    0.0132  3.14
ano_nac_corr                   0.0402    1.0410   0.0136    0.0129  3.12
susinidum_oh                   0.0310    1.0315   0.2196    0.2160  0.14
susinidum_coc                  0.0954    1.1001   0.2557    0.2466  0.39
susinidum_pbc                  0.2317    1.2608   0.2497    0.2416  0.96
susinidum_mar                  0.0550    1.0566   0.2215    0.2192  0.25
psycom_dum_with               -0.0615    0.9403   0.0666    0.0678 -0.91
psycom_dum_study              -0.1058    0.8996   0.0799    0.0743 -1.42
freq_cons_dum_5day             0.0800    1.0832   0.0674    0.0653  1.22
cond_oc_dum_2inact            -0.0123    0.9878   0.0959    0.0935 -0.13
cond_oc_dum_3unemp            -0.0226    0.9777   0.0695    0.0705 -0.32
susprindum_oh                 -0.4384    0.6451   0.2856    0.2298 -1.91
susprindum_coc                -0.4450    0.6408   0.2898    0.2350 -1.89
susprindum_pbc                -0.4805    0.6185   0.2855    0.2282 -2.11
susprindum_mar                -0.4434    0.6418   0.2988    0.2540 -1.75
                              Pr(>|z|)   
tr_outcome.lag                  0.4637   
less_90d_tr1.lag                0.0839 . 
log_dias_treat_imp_sin_na.lag   0.0460 * 
comp_bpsc_y3_severe.lag         0.8011   
policonsumo2.lag                0.2172   
edad_al_ing_1                   0.0017 **
ano_nac_corr                    0.0018 **
susinidum_oh                    0.8859   
susinidum_coc                   0.6989   
susinidum_pbc                   0.3374   
susinidum_mar                   0.8018   
psycom_dum_with                 0.3641   
psycom_dum_study                0.1543   
freq_cons_dum_5day              0.2208   
cond_oc_dum_2inact              0.8957   
cond_oc_dum_3unemp              0.7489   
susprindum_oh                   0.0564 . 
susprindum_coc                  0.0582 . 
susprindum_pbc                  0.0352 * 
susprindum_mar                  0.0809 . 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    1.249      0.801     0.689     2.263
less_90d_tr1.lag                  2.311      0.433     0.894     5.972
log_dias_treat_imp_sin_na.lag     1.513      0.661     1.007     2.273
comp_bpsc_y3_severe.lag           1.080      0.926     0.594     1.963
policonsumo2.lag                  1.460      0.685     0.800     2.661
edad_al_ing_1                     1.042      0.959     1.016     1.069
ano_nac_corr                      1.041      0.961     1.015     1.068
susinidum_oh                      1.031      0.969     0.675     1.575
susinidum_coc                     1.100      0.909     0.678     1.784
susinidum_pbc                     1.261      0.793     0.785     2.024
susinidum_mar                     1.057      0.946     0.688     1.623
psycom_dum_with                   0.940      1.063     0.823     1.074
psycom_dum_study                  0.900      1.112     0.778     1.041
freq_cons_dum_5day                1.083      0.923     0.953     1.231
cond_oc_dum_2inact                0.988      1.012     0.822     1.187
cond_oc_dum_3unemp                0.978      1.023     0.851     1.123
susprindum_oh                     0.645      1.550     0.411     1.012
susprindum_coc                    0.641      1.561     0.404     1.016
susprindum_pbc                    0.618      1.617     0.395     0.967
susprindum_mar                    0.642      1.558     0.390     1.056

Concordance= 0.552  (se = 0.009 )
Likelihood ratio test= 29  on 20 df,   p=0.09
Wald test            = 43.1  on 20 df,   p=0.002
Score (logrank) test = 27.6  on 20 df,   p=0.1,   Robust = 32  p=0.04

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.201   0.314   0.343   0.349   0.374   1.062 
[1] "Modality: GP residential\nTime interval: (20,30]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 612, number of events= 612 
   (578 observations deleted due to missingness)

                                 coef exp(coef) se(coef) robust se     z
tr_outcome.lag                 0.0126    1.0127   0.5116    0.2984  0.04
less_90d_tr1.lag               0.3437    1.4102   0.4858    0.3058  1.12
log_dias_treat_imp_sin_na.lag -0.1050    0.9003   0.2077    0.1210 -0.87
comp_bpsc_y3_severe.lag        0.4596    1.5835   0.4627    0.2917  1.58
policonsumo2.lag               0.5207    1.6832   0.5941    0.3723  1.40
edad_al_ing_1                  0.0165    1.0167   0.0203    0.0182  0.91
ano_nac_corr                   0.0213    1.0215   0.0200    0.0183  1.16
susinidum_oh                  -0.4668    0.6270   0.3163    0.2554 -1.83
susinidum_coc                 -0.0377    0.9630   0.3617    0.2968 -0.13
susinidum_pbc                 -0.2989    0.7416   0.3315    0.2661 -1.12
susinidum_mar                 -0.4887    0.6134   0.3163    0.2526 -1.94
psycom_dum_with               -0.1009    0.9040   0.0943    0.0976 -1.03
psycom_dum_study              -0.0178    0.9824   0.1338    0.1232 -0.14
freq_cons_dum_5day             0.1393    1.1494   0.0949    0.0940  1.48
cond_oc_dum_2inact            -0.0755    0.9272   0.1501    0.1472 -0.51
cond_oc_dum_3unemp            -0.0611    0.9407   0.1294    0.1260 -0.49
susprindum_oh                  0.8357    2.3065   0.4654    0.4046  2.07
susprindum_coc                 0.8076    2.2426   0.4702    0.4086  1.98
susprindum_pbc                 0.8110    2.2503   0.4570    0.3925  2.07
susprindum_mar                 0.5686    1.7657   0.5253    0.4503  1.26
                              Pr(>|z|)  
tr_outcome.lag                   0.966  
less_90d_tr1.lag                 0.261  
log_dias_treat_imp_sin_na.lag    0.386  
comp_bpsc_y3_severe.lag          0.115  
policonsumo2.lag                 0.162  
edad_al_ing_1                    0.365  
ano_nac_corr                     0.246  
susinidum_oh                     0.068 .
susinidum_coc                    0.899  
susinidum_pbc                    0.261  
susinidum_mar                    0.053 .
psycom_dum_with                  0.301  
psycom_dum_study                 0.885  
freq_cons_dum_5day               0.138  
cond_oc_dum_2inact               0.608  
cond_oc_dum_3unemp               0.628  
susprindum_oh                    0.039 *
susprindum_coc                   0.048 *
susprindum_pbc                   0.039 *
susprindum_mar                   0.207  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    1.013      0.987     0.564      1.82
less_90d_tr1.lag                  1.410      0.709     0.774      2.57
log_dias_treat_imp_sin_na.lag     0.900      1.111     0.710      1.14
comp_bpsc_y3_severe.lag           1.583      0.632     0.894      2.81
policonsumo2.lag                  1.683      0.594     0.811      3.49
edad_al_ing_1                     1.017      0.984     0.981      1.05
ano_nac_corr                      1.022      0.979     0.985      1.06
susinidum_oh                      0.627      1.595     0.380      1.03
susinidum_coc                     0.963      1.038     0.538      1.72
susinidum_pbc                     0.742      1.348     0.440      1.25
susinidum_mar                     0.613      1.630     0.374      1.01
psycom_dum_with                   0.904      1.106     0.747      1.09
psycom_dum_study                  0.982      1.018     0.772      1.25
freq_cons_dum_5day                1.149      0.870     0.956      1.38
cond_oc_dum_2inact                0.927      1.078     0.695      1.24
cond_oc_dum_3unemp                0.941      1.063     0.735      1.20
susprindum_oh                     2.307      0.434     1.044      5.10
susprindum_coc                    2.243      0.446     1.007      5.00
susprindum_pbc                    2.250      0.444     1.043      4.86
susprindum_mar                    1.766      0.566     0.730      4.27

Concordance= 0.549  (se = 0.013 )
Likelihood ratio test= 30.4  on 20 df,   p=0.06
Wald test            = 47.8  on 20 df,   p=5e-04
Score (logrank) test = 28.6  on 20 df,   p=0.1,   Robust = 28.3  p=0.1

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.089   0.169   0.184   0.191   0.201   0.559 
[1] "Modality: WO intensive ambulatory\nTime interval: (20,30]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 247, number of events= 247 
   (235 observations deleted due to missingness)

                                  coef exp(coef) se(coef) robust se     z
tr_outcome.lag                 0.40479   1.49898  1.04300   0.48419  0.84
less_90d_tr1.lag               0.08277   1.08629  0.91450   0.49716  0.17
log_dias_treat_imp_sin_na.lag  0.19472   1.21497  0.76258   0.39201  0.50
comp_bpsc_y3_severe.lag        0.43809   1.54974  0.87445   0.57240  0.77
policonsumo2.lag               0.97602   2.65387  0.77855   0.63265  1.54
edad_al_ing_1                 -0.01578   0.98434  0.03117   0.02972 -0.53
ano_nac_corr                  -0.01833   0.98184  0.02929   0.02678 -0.68
susinidum_oh                  -0.54823   0.57797  0.55667   0.47227 -1.16
susinidum_coc                 -0.41163   0.66257  0.63174   0.54671 -0.75
susinidum_pbc                 -0.72558   0.48404  0.59230   0.50021 -1.45
susinidum_mar                 -0.44927   0.63809  0.56526   0.47497 -0.95
psycom_dum_with                0.02344   1.02371  0.14638   0.14068  0.17
psycom_dum_study               0.30425   1.35561  0.23596   0.25941  1.17
freq_cons_dum_5day            -0.25259   0.77679  0.14603   0.13977 -1.81
cond_oc_dum_2inact            -0.05708   0.94452  0.17429   0.15677 -0.36
cond_oc_dum_3unemp            -0.00650   0.99352  0.18484   0.18418 -0.04
susprindum_oh                 -0.43071   0.65005  0.45904   0.35991 -1.20
susprindum_coc                 0.12234   1.13014  0.47224   0.39638  0.31
susprindum_pbc                -0.00815   0.99189  0.46215   0.37515 -0.02
susprindum_mar                -0.01805   0.98211  0.51912   0.40588 -0.04
                              Pr(>|z|)  
tr_outcome.lag                   0.403  
less_90d_tr1.lag                 0.868  
log_dias_treat_imp_sin_na.lag    0.619  
comp_bpsc_y3_severe.lag          0.444  
policonsumo2.lag                 0.123  
edad_al_ing_1                    0.595  
ano_nac_corr                     0.494  
susinidum_oh                     0.246  
susinidum_coc                    0.452  
susinidum_pbc                    0.147  
susinidum_mar                    0.344  
psycom_dum_with                  0.868  
psycom_dum_study                 0.241  
freq_cons_dum_5day               0.071 .
cond_oc_dum_2inact               0.716  
cond_oc_dum_3unemp               0.972  
susprindum_oh                    0.231  
susprindum_coc                   0.758  
susprindum_pbc                   0.983  
susprindum_mar                   0.965  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    1.499      0.667     0.580      3.87
less_90d_tr1.lag                  1.086      0.921     0.410      2.88
log_dias_treat_imp_sin_na.lag     1.215      0.823     0.563      2.62
comp_bpsc_y3_severe.lag           1.550      0.645     0.505      4.76
policonsumo2.lag                  2.654      0.377     0.768      9.17
edad_al_ing_1                     0.984      1.016     0.929      1.04
ano_nac_corr                      0.982      1.018     0.932      1.03
susinidum_oh                      0.578      1.730     0.229      1.46
susinidum_coc                     0.663      1.509     0.227      1.93
susinidum_pbc                     0.484      2.066     0.182      1.29
susinidum_mar                     0.638      1.567     0.252      1.62
psycom_dum_with                   1.024      0.977     0.777      1.35
psycom_dum_study                  1.356      0.738     0.815      2.25
freq_cons_dum_5day                0.777      1.287     0.591      1.02
cond_oc_dum_2inact                0.945      1.059     0.695      1.28
cond_oc_dum_3unemp                0.994      1.007     0.692      1.43
susprindum_oh                     0.650      1.538     0.321      1.32
susprindum_coc                    1.130      0.885     0.520      2.46
susprindum_pbc                    0.992      1.008     0.475      2.07
susprindum_mar                    0.982      1.018     0.443      2.18

Concordance= 0.589  (se = 0.021 )
Likelihood ratio test= 17.7  on 20 df,   p=0.6
Wald test            = 28.8  on 20 df,   p=0.09
Score (logrank) test = 17.2  on 20 df,   p=0.6,   Robust = 21  p=0.4

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.143   0.236   0.286   0.305   0.360   1.005 
[1] "Modality: WO residential\nTime interval: (30,50]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 374, number of events= 374 
   (332 observations deleted due to missingness)

                                 coef exp(coef) se(coef) robust se     z
tr_outcome.lag                -0.1910    0.8261   0.3716    0.1834 -1.04
less_90d_tr1.lag               0.3183    1.3749   0.2421    0.1323  2.41
log_dias_treat_imp_sin_na.lag -0.0582    0.9434   0.0852    0.0303 -1.92
comp_bpsc_y3_severe.lag        0.2192    1.2451   0.4037    0.1521  1.44
policonsumo2.lag              -0.1290    0.8789   0.5011    0.1685 -0.77
edad_al_ing_1                  0.0669    1.0692   0.0311    0.0320  2.09
ano_nac_corr                   0.0667    1.0689   0.0305    0.0315  2.12
susinidum_oh                   1.1813    3.2587   0.5603    0.3102  3.81
susinidum_coc                  1.4419    4.2287   0.6052    0.3416  4.22
susinidum_pbc                  1.1537    3.1698   0.5694    0.3223  3.58
susinidum_mar                  1.1777    3.2468   0.5616    0.3072  3.83
psycom_dum_with               -0.0454    0.9557   0.1506    0.1451 -0.31
psycom_dum_study               0.0300    1.0305   0.1714    0.1609  0.19
freq_cons_dum_5day            -0.0269    0.9734   0.1255    0.1198 -0.22
cond_oc_dum_2inact            -0.1698    0.8439   0.2263    0.1750 -0.97
cond_oc_dum_3unemp            -0.2563    0.7739   0.2304    0.1896 -1.35
susprindum_oh                 -0.8702    0.4189   0.3963    0.2483 -3.50
susprindum_coc                -0.9727    0.3781   0.4019    0.2531 -3.84
susprindum_pbc                -0.8575    0.4242   0.3773    0.2169 -3.95
susprindum_mar                -0.4442    0.6413   0.5670    0.3156 -1.41
                              Pr(>|z|)    
tr_outcome.lag                 0.29773    
less_90d_tr1.lag               0.01615 *  
log_dias_treat_imp_sin_na.lag  0.05447 .  
comp_bpsc_y3_severe.lag        0.14948    
policonsumo2.lag               0.44375    
edad_al_ing_1                  0.03654 *  
ano_nac_corr                   0.03411 *  
susinidum_oh                   0.00014 ***
susinidum_coc                  2.4e-05 ***
susinidum_pbc                  0.00034 ***
susinidum_mar                  0.00013 ***
psycom_dum_with                0.75448    
psycom_dum_study               0.85194    
freq_cons_dum_5day             0.82209    
cond_oc_dum_2inact             0.33203    
cond_oc_dum_3unemp             0.17642    
susprindum_oh                  0.00046 ***
susprindum_coc                 0.00012 ***
susprindum_pbc                 7.7e-05 ***
susprindum_mar                 0.15934    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    0.826      1.210     0.577     1.184
less_90d_tr1.lag                  1.375      0.727     1.061     1.782
log_dias_treat_imp_sin_na.lag     0.943      1.060     0.889     1.001
comp_bpsc_y3_severe.lag           1.245      0.803     0.924     1.677
policonsumo2.lag                  0.879      1.138     0.632     1.223
edad_al_ing_1                     1.069      0.935     1.004     1.138
ano_nac_corr                      1.069      0.935     1.005     1.137
susinidum_oh                      3.259      0.307     1.774     5.985
susinidum_coc                     4.229      0.236     2.165     8.261
susinidum_pbc                     3.170      0.315     1.685     5.962
susinidum_mar                     3.247      0.308     1.778     5.928
psycom_dum_with                   0.956      1.046     0.719     1.270
psycom_dum_study                  1.030      0.970     0.752     1.413
freq_cons_dum_5day                0.973      1.027     0.770     1.231
cond_oc_dum_2inact                0.844      1.185     0.599     1.189
cond_oc_dum_3unemp                0.774      1.292     0.534     1.122
susprindum_oh                     0.419      2.387     0.257     0.681
susprindum_coc                    0.378      2.645     0.230     0.621
susprindum_pbc                    0.424      2.357     0.277     0.649
susprindum_mar                    0.641      1.559     0.345     1.191

Concordance= 0.558  (se = 0.016 )
Likelihood ratio test= 21.5  on 20 df,   p=0.4
Wald test            = 72.6  on 20 df,   p=7e-08
Score (logrank) test = 20.9  on 20 df,   p=0.4,   Robust = 31.6  p=0.05

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.245   0.662   0.747   0.776   0.873   2.800 
[1] "Modality: GP intensive ambulatory\nTime interval: (30,50]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 1813, number of events= 1813 
   (1667 observations deleted due to missingness)

                                  coef exp(coef) se(coef) robust se     z
tr_outcome.lag                 0.14332   1.15409  0.22733   0.15475  0.93
less_90d_tr1.lag               0.07829   1.08144  0.15134   0.13201  0.59
log_dias_treat_imp_sin_na.lag -0.07846   0.92454  0.06789   0.02680 -2.93
comp_bpsc_y3_severe.lag        0.22928   1.25770  0.14756   0.12873  1.78
policonsumo2.lag              -0.04683   0.95425  0.22188   0.15928 -0.29
edad_al_ing_1                  0.04965   1.05091  0.01264   0.01231  4.03
ano_nac_corr                   0.05133   1.05267  0.01264   0.01252  4.10
susinidum_oh                  -0.22128   0.80149  0.18650   0.15174 -1.46
susinidum_coc                 -0.10405   0.90118  0.21627   0.18393 -0.57
susinidum_pbc                 -0.29351   0.74564  0.20573   0.17919 -1.64
susinidum_mar                 -0.26823   0.76473  0.18907   0.15569 -1.72
psycom_dum_with                0.03134   1.03183  0.05337   0.05347  0.59
psycom_dum_study              -0.02588   0.97445  0.07087   0.07111 -0.36
freq_cons_dum_5day             0.02118   1.02141  0.04865   0.04795  0.44
cond_oc_dum_2inact            -0.00426   0.99575  0.07192   0.07471 -0.06
cond_oc_dum_3unemp             0.09166   1.09600  0.05231   0.05118  1.79
susprindum_oh                  0.02147   1.02171  0.21852   0.17270  0.12
susprindum_coc                 0.07592   1.07887  0.22263   0.17813  0.43
susprindum_pbc                 0.05355   1.05501  0.21734   0.17184  0.31
susprindum_mar                 0.05373   1.05520  0.24219   0.19594  0.27
                              Pr(>|z|)    
tr_outcome.lag                  0.3544    
less_90d_tr1.lag                0.5531    
log_dias_treat_imp_sin_na.lag   0.0034 ** 
comp_bpsc_y3_severe.lag         0.0749 .  
policonsumo2.lag                0.7687    
edad_al_ing_1                  5.5e-05 ***
ano_nac_corr                   4.1e-05 ***
susinidum_oh                    0.1448    
susinidum_coc                   0.5716    
susinidum_pbc                   0.1014    
susinidum_mar                   0.0849 .  
psycom_dum_with                 0.5579    
psycom_dum_study                0.7159    
freq_cons_dum_5day              0.6586    
cond_oc_dum_2inact              0.9545    
cond_oc_dum_3unemp              0.0733 .  
susprindum_oh                   0.9011    
susprindum_coc                  0.6700    
susprindum_pbc                  0.7553    
susprindum_mar                  0.7839    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    1.154      0.866     0.852     1.563
less_90d_tr1.lag                  1.081      0.925     0.835     1.401
log_dias_treat_imp_sin_na.lag     0.925      1.082     0.877     0.974
comp_bpsc_y3_severe.lag           1.258      0.795     0.977     1.619
policonsumo2.lag                  0.954      1.048     0.698     1.304
edad_al_ing_1                     1.051      0.952     1.026     1.077
ano_nac_corr                      1.053      0.950     1.027     1.079
susinidum_oh                      0.801      1.248     0.595     1.079
susinidum_coc                     0.901      1.110     0.628     1.292
susinidum_pbc                     0.746      1.341     0.525     1.059
susinidum_mar                     0.765      1.308     0.564     1.038
psycom_dum_with                   1.032      0.969     0.929     1.146
psycom_dum_study                  0.974      1.026     0.848     1.120
freq_cons_dum_5day                1.021      0.979     0.930     1.122
cond_oc_dum_2inact                0.996      1.004     0.860     1.153
cond_oc_dum_3unemp                1.096      0.912     0.991     1.212
susprindum_oh                     1.022      0.979     0.728     1.433
susprindum_coc                    1.079      0.927     0.761     1.530
susprindum_pbc                    1.055      0.948     0.753     1.478
susprindum_mar                    1.055      0.948     0.719     1.549

Concordance= 0.541  (se = 0.008 )
Likelihood ratio test= 38.2  on 20 df,   p=0.008
Wald test            = 52.9  on 20 df,   p=8e-05
Score (logrank) test = 37.1  on 20 df,   p=0.01,   Robust = 47.9  p=4e-04

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.467   0.699   0.763   0.780   0.840   1.641 
[1] "Modality: basic ambulatory\nTime interval: (30,50]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 1568, number of events= 1568 
   (1465 observations deleted due to missingness)

                                 coef exp(coef) se(coef) robust se     z
tr_outcome.lag                -0.2396    0.7869   0.2605    0.1952 -1.23
less_90d_tr1.lag               0.1666    1.1813   0.2345    0.2055  0.81
log_dias_treat_imp_sin_na.lag  0.0305    1.0310   0.1362    0.1365  0.22
comp_bpsc_y3_severe.lag        0.1990    1.2201   0.2024    0.1578  1.26
policonsumo2.lag               0.2099    1.2336   0.2926    0.2411  0.87
edad_al_ing_1                  0.0621    1.0640   0.0137    0.0134  4.64
ano_nac_corr                   0.0617    1.0636   0.0134    0.0134  4.60
susinidum_oh                   0.2714    1.3117   0.1894    0.1984  1.37
susinidum_coc                  0.1121    1.1186   0.2247    0.2401  0.47
susinidum_pbc                  0.2334    1.2629   0.2212    0.2241  1.04
susinidum_mar                  0.3088    1.3618   0.1922    0.2027  1.52
psycom_dum_with                0.0789    1.0821   0.0567    0.0574  1.38
psycom_dum_study               0.0741    1.0769   0.0745    0.0720  1.03
freq_cons_dum_5day            -0.0672    0.9350   0.0585    0.0580 -1.16
cond_oc_dum_2inact            -0.0729    0.9297   0.0799    0.0772 -0.94
cond_oc_dum_3unemp             0.0917    1.0960   0.0615    0.0612  1.50
susprindum_oh                 -0.1605    0.8517   0.2267    0.1864 -0.86
susprindum_coc                -0.1131    0.8930   0.2313    0.1932 -0.59
susprindum_pbc                -0.1756    0.8390   0.2260    0.1865 -0.94
susprindum_mar                -0.2675    0.7653   0.2436    0.2004 -1.33
                              Pr(>|z|)    
tr_outcome.lag                    0.22    
less_90d_tr1.lag                  0.42    
log_dias_treat_imp_sin_na.lag     0.82    
comp_bpsc_y3_severe.lag           0.21    
policonsumo2.lag                  0.38    
edad_al_ing_1                  3.5e-06 ***
ano_nac_corr                   4.1e-06 ***
susinidum_oh                      0.17    
susinidum_coc                     0.64    
susinidum_pbc                     0.30    
susinidum_mar                     0.13    
psycom_dum_with                   0.17    
psycom_dum_study                  0.30    
freq_cons_dum_5day                0.25    
cond_oc_dum_2inact                0.34    
cond_oc_dum_3unemp                0.13    
susprindum_oh                     0.39    
susprindum_coc                    0.56    
susprindum_pbc                    0.35    
susprindum_mar                    0.18    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    0.787      1.271     0.537      1.15
less_90d_tr1.lag                  1.181      0.847     0.790      1.77
log_dias_treat_imp_sin_na.lag     1.031      0.970     0.789      1.35
comp_bpsc_y3_severe.lag           1.220      0.820     0.896      1.66
policonsumo2.lag                  1.234      0.811     0.769      1.98
edad_al_ing_1                     1.064      0.940     1.036      1.09
ano_nac_corr                      1.064      0.940     1.036      1.09
susinidum_oh                      1.312      0.762     0.889      1.94
susinidum_coc                     1.119      0.894     0.699      1.79
susinidum_pbc                     1.263      0.792     0.814      1.96
susinidum_mar                     1.362      0.734     0.915      2.03
psycom_dum_with                   1.082      0.924     0.967      1.21
psycom_dum_study                  1.077      0.929     0.935      1.24
freq_cons_dum_5day                0.935      1.070     0.835      1.05
cond_oc_dum_2inact                0.930      1.076     0.799      1.08
cond_oc_dum_3unemp                1.096      0.912     0.972      1.24
susprindum_oh                     0.852      1.174     0.591      1.23
susprindum_coc                    0.893      1.120     0.612      1.30
susprindum_pbc                    0.839      1.192     0.582      1.21
susprindum_mar                    0.765      1.307     0.517      1.13

Concordance= 0.552  (se = 0.008 )
Likelihood ratio test= 45.3  on 20 df,   p=0.001
Wald test            = 52  on 20 df,   p=1e-04
Score (logrank) test = 44.3  on 20 df,   p=0.001,   Robust = 50  p=2e-04

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.412   0.552   0.624   0.638   0.705   1.289 
[1] "Modality: GP residential\nTime interval: (30,50]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 756, number of events= 756 
   (698 observations deleted due to missingness)

                                   coef exp(coef)  se(coef) robust se     z
tr_outcome.lag                 0.070705  1.073265  0.311271  0.180516  0.39
less_90d_tr1.lag              -0.260209  0.770890  0.245527  0.187185 -1.39
log_dias_treat_imp_sin_na.lag -0.055768  0.945758  0.100114  0.081450 -0.68
comp_bpsc_y3_severe.lag        0.785267  2.192992  0.372942  0.268580  2.92
policonsumo2.lag              -0.642543  0.525953  0.355505  0.222510 -2.89
edad_al_ing_1                  0.081089  1.084468  0.020322  0.018749  4.33
ano_nac_corr                   0.084354  1.088014  0.020160  0.018604  4.53
susinidum_oh                  -0.029896  0.970547  0.247800  0.318855 -0.09
susinidum_coc                  0.388930  1.475401  0.325541  0.367282  1.06
susinidum_pbc                  0.104636  1.110306  0.265472  0.327583  0.32
susinidum_mar                 -0.000313  0.999687  0.247905  0.317410  0.00
psycom_dum_with               -0.113781  0.892453  0.084083  0.083715 -1.36
psycom_dum_study              -0.126070  0.881553  0.117167  0.114303 -1.10
freq_cons_dum_5day             0.007669  1.007698  0.086974  0.081765  0.09
cond_oc_dum_2inact             0.155831  1.168629  0.138600  0.144992  1.07
cond_oc_dum_3unemp             0.239732  1.270908  0.114408  0.107619  2.23
susprindum_oh                  0.168151  1.183116  0.337103  0.282967  0.59
susprindum_coc                -0.137028  0.871946  0.344384  0.295404 -0.46
susprindum_pbc                -0.012679  0.987401  0.326303  0.271486 -0.05
susprindum_mar                 0.122759  1.130612  0.377851  0.322122  0.38
                              Pr(>|z|)    
tr_outcome.lag                  0.6953    
less_90d_tr1.lag                0.1645    
log_dias_treat_imp_sin_na.lag   0.4935    
comp_bpsc_y3_severe.lag         0.0035 ** 
policonsumo2.lag                0.0039 ** 
edad_al_ing_1                  1.5e-05 ***
ano_nac_corr                   5.8e-06 ***
susinidum_oh                    0.9253    
susinidum_coc                   0.2896    
susinidum_pbc                   0.7494    
susinidum_mar                   0.9992    
psycom_dum_with                 0.1741    
psycom_dum_study                0.2700    
freq_cons_dum_5day              0.9253    
cond_oc_dum_2inact              0.2825    
cond_oc_dum_3unemp              0.0259 *  
susprindum_oh                   0.5523    
susprindum_coc                  0.6427    
susprindum_pbc                  0.9628    
susprindum_mar                  0.7031    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    1.073      0.932     0.753     1.529
less_90d_tr1.lag                  0.771      1.297     0.534     1.113
log_dias_treat_imp_sin_na.lag     0.946      1.057     0.806     1.109
comp_bpsc_y3_severe.lag           2.193      0.456     1.295     3.712
policonsumo2.lag                  0.526      1.901     0.340     0.813
edad_al_ing_1                     1.084      0.922     1.045     1.125
ano_nac_corr                      1.088      0.919     1.049     1.128
susinidum_oh                      0.971      1.030     0.520     1.813
susinidum_coc                     1.475      0.678     0.718     3.031
susinidum_pbc                     1.110      0.901     0.584     2.110
susinidum_mar                     1.000      1.000     0.537     1.862
psycom_dum_with                   0.892      1.121     0.757     1.052
psycom_dum_study                  0.882      1.134     0.705     1.103
freq_cons_dum_5day                1.008      0.992     0.858     1.183
cond_oc_dum_2inact                1.169      0.856     0.880     1.553
cond_oc_dum_3unemp                1.271      0.787     1.029     1.569
susprindum_oh                     1.183      0.845     0.679     2.060
susprindum_coc                    0.872      1.147     0.489     1.556
susprindum_pbc                    0.987      1.013     0.580     1.681
susprindum_mar                    1.131      0.884     0.601     2.126

Concordance= 0.567  (se = 0.012 )
Likelihood ratio test= 35.4  on 20 df,   p=0.02
Wald test            = 54.3  on 20 df,   p=5e-05
Score (logrank) test = 35.7  on 20 df,   p=0.02,   Robust = 43.4  p=0.002

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.356   0.790   0.915   0.932   1.057   2.345 
[1] "Modality: WO intensive ambulatory\nTime interval: (30,50]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 272, number of events= 272 
   (247 observations deleted due to missingness)

                                 coef exp(coef) se(coef) robust se     z
tr_outcome.lag                -0.5814    0.5591   0.6865    0.4366 -1.33
less_90d_tr1.lag               1.2589    3.5215   0.4292    0.3305  3.81
log_dias_treat_imp_sin_na.lag  0.1274    1.1358   0.1779    0.0937  1.36
comp_bpsc_y3_severe.lag       -0.8336    0.4345   0.6234    0.4921 -1.69
policonsumo2.lag               0.1199    1.1274   0.6456    0.5892  0.20
edad_al_ing_1                  0.0296    1.0301   0.0335    0.0305  0.97
ano_nac_corr                   0.0270    1.0274   0.0327    0.0309  0.87
susinidum_oh                  -0.6095    0.5436   0.4208    0.4329 -1.41
susinidum_coc                 -0.4464    0.6399   0.5109    0.5645 -0.79
susinidum_pbc                 -0.3263    0.7216   0.4432    0.4572 -0.71
susinidum_mar                 -0.3963    0.6728   0.4257    0.4399 -0.90
psycom_dum_with                0.1430    1.1537   0.1442    0.1415  1.01
psycom_dum_study              -0.2141    0.8073   0.2120    0.1933 -1.11
freq_cons_dum_5day             0.0414    1.0423   0.1349    0.1394  0.30
cond_oc_dum_2inact             0.0616    1.0635   0.1674    0.1655  0.37
cond_oc_dum_3unemp             0.1078    1.1138   0.1722    0.1674  0.64
susprindum_oh                 -0.4157    0.6599   0.4391    0.2908 -1.43
susprindum_coc                -0.1702    0.8435   0.4485    0.2815 -0.60
susprindum_pbc                -0.3561    0.7004   0.4249    0.2678 -1.33
susprindum_mar                -0.1563    0.8553   0.4773    0.3353 -0.47
                              Pr(>|z|)    
tr_outcome.lag                 0.18298    
less_90d_tr1.lag               0.00014 ***
log_dias_treat_imp_sin_na.lag  0.17413    
comp_bpsc_y3_severe.lag        0.09030 .  
policonsumo2.lag               0.83876    
edad_al_ing_1                  0.33056    
ano_nac_corr                   0.38200    
susinidum_oh                   0.15914    
susinidum_coc                  0.42909    
susinidum_pbc                  0.47550    
susinidum_mar                  0.36775    
psycom_dum_with                0.31218    
psycom_dum_study               0.26805    
freq_cons_dum_5day             0.76662    
cond_oc_dum_2inact             0.70979    
cond_oc_dum_3unemp             0.51948    
susprindum_oh                  0.15284    
susprindum_coc                 0.54539    
susprindum_pbc                 0.18369    
susprindum_mar                 0.64124    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    0.559      1.788     0.238      1.32
less_90d_tr1.lag                  3.522      0.284     1.842      6.73
log_dias_treat_imp_sin_na.lag     1.136      0.880     0.945      1.36
comp_bpsc_y3_severe.lag           0.434      2.302     0.166      1.14
policonsumo2.lag                  1.127      0.887     0.355      3.58
edad_al_ing_1                     1.030      0.971     0.970      1.09
ano_nac_corr                      1.027      0.973     0.967      1.09
susinidum_oh                      0.544      1.839     0.233      1.27
susinidum_coc                     0.640      1.563     0.212      1.93
susinidum_pbc                     0.722      1.386     0.295      1.77
susinidum_mar                     0.673      1.486     0.284      1.59
psycom_dum_with                   1.154      0.867     0.874      1.52
psycom_dum_study                  0.807      1.239     0.553      1.18
freq_cons_dum_5day                1.042      0.959     0.793      1.37
cond_oc_dum_2inact                1.064      0.940     0.769      1.47
cond_oc_dum_3unemp                1.114      0.898     0.802      1.55
susprindum_oh                     0.660      1.515     0.373      1.17
susprindum_coc                    0.843      1.186     0.486      1.46
susprindum_pbc                    0.700      1.428     0.414      1.18
susprindum_mar                    0.855      1.169     0.443      1.65

Concordance= 0.564  (se = 0.018 )
Likelihood ratio test= 23.1  on 20 df,   p=0.3
Wald test            = 48.5  on 20 df,   p=4e-04
Score (logrank) test = 21.2  on 20 df,   p=0.4,   Robust = 29.2  p=0.08

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   1.01    1.69    2.07    2.19    2.44    8.87 
[1] "Modality: WO residential\nTime interval: (50,70]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 179, number of events= 179 
   (161 observations deleted due to missingness)

                                  coef exp(coef) se(coef) robust se     z
tr_outcome.lag                 0.11718   1.12432  0.61305   0.54534  0.21
less_90d_tr1.lag               1.02457   2.78591  1.06311   0.67874  1.51
log_dias_treat_imp_sin_na.lag  0.11300   1.11963  0.72792   0.40985  0.28
comp_bpsc_y3_severe.lag        0.42784   1.53394  0.84312   0.62478  0.68
policonsumo2.lag               0.64360   1.90332  0.56869   0.31440  2.05
edad_al_ing_1                  0.03070   1.03118  0.05649   0.05622  0.55
ano_nac_corr                   0.04493   1.04596  0.05520   0.05505  0.82
susinidum_oh                  -0.87341   0.41752  0.63993   0.44994 -1.94
susinidum_coc                 -0.55173   0.57595  0.72746   0.52218 -1.06
susinidum_pbc                 -0.76520   0.46524  0.65960   0.46848 -1.63
susinidum_mar                 -0.42396   0.65445  0.63809   0.47701 -0.89
psycom_dum_with               -0.57471   0.56287  0.22561   0.21753 -2.64
psycom_dum_study              -0.65671   0.51856  0.26267   0.27317 -2.40
freq_cons_dum_5day             0.01414   1.01425  0.20775   0.20475  0.07
cond_oc_dum_2inact            -0.12188   0.88525  0.30041   0.23571 -0.52
cond_oc_dum_3unemp            -0.01956   0.98063  0.30449   0.23485 -0.08
susprindum_oh                  0.05359   1.05505  0.52266   0.41524  0.13
susprindum_coc                 0.24811   1.28159  0.53273   0.42393  0.59
susprindum_pbc                 0.05261   1.05402  0.48620   0.39046  0.13
susprindum_mar                -0.00979   0.99026  0.77683   0.59414 -0.02
                              Pr(>|z|)   
tr_outcome.lag                  0.8299   
less_90d_tr1.lag                0.1312   
log_dias_treat_imp_sin_na.lag   0.7828   
comp_bpsc_y3_severe.lag         0.4935   
policonsumo2.lag                0.0406 * 
edad_al_ing_1                   0.5850   
ano_nac_corr                    0.4143   
susinidum_oh                    0.0522 . 
susinidum_coc                   0.2907   
susinidum_pbc                   0.1024   
susinidum_mar                   0.3741   
psycom_dum_with                 0.0082 **
psycom_dum_study                0.0162 * 
freq_cons_dum_5day              0.9449   
cond_oc_dum_2inact              0.6051   
cond_oc_dum_3unemp              0.9336   
susprindum_oh                   0.8973   
susprindum_coc                  0.5584   
susprindum_pbc                  0.8928   
susprindum_mar                  0.9869   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    1.124      0.889     0.386     3.274
less_90d_tr1.lag                  2.786      0.359     0.737    10.537
log_dias_treat_imp_sin_na.lag     1.120      0.893     0.501     2.500
comp_bpsc_y3_severe.lag           1.534      0.652     0.451     5.219
policonsumo2.lag                  1.903      0.525     1.028     3.525
edad_al_ing_1                     1.031      0.970     0.924     1.151
ano_nac_corr                      1.046      0.956     0.939     1.165
susinidum_oh                      0.418      2.395     0.173     1.008
susinidum_coc                     0.576      1.736     0.207     1.603
susinidum_pbc                     0.465      2.149     0.186     1.165
susinidum_mar                     0.654      1.528     0.257     1.667
psycom_dum_with                   0.563      1.777     0.367     0.862
psycom_dum_study                  0.519      1.928     0.304     0.886
freq_cons_dum_5day                1.014      0.986     0.679     1.515
cond_oc_dum_2inact                0.885      1.130     0.558     1.405
cond_oc_dum_3unemp                0.981      1.020     0.619     1.554
susprindum_oh                     1.055      0.948     0.468     2.381
susprindum_coc                    1.282      0.780     0.558     2.942
susprindum_pbc                    1.054      0.949     0.490     2.266
susprindum_mar                    0.990      1.010     0.309     3.173

Concordance= 0.606  (se = 0.024 )
Likelihood ratio test= 31.8  on 20 df,   p=0.05
Wald test            = 53.7  on 20 df,   p=6e-05
Score (logrank) test = 29.4  on 20 df,   p=0.08,   Robust = 34  p=0.03

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.131   0.283   0.343   0.426   0.481   2.311 
[1] "Modality: GP intensive ambulatory\nTime interval: (50,70]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 952, number of events= 952 
   (882 observations deleted due to missingness)

                                  coef exp(coef) se(coef) robust se     z
tr_outcome.lag                 0.14373   1.15457  0.40208   0.23848  0.60
less_90d_tr1.lag               0.04075   1.04160  0.26896   0.18882  0.22
log_dias_treat_imp_sin_na.lag -0.06494   0.93712  0.18076   0.14215 -0.46
comp_bpsc_y3_severe.lag        0.24369   1.27594  0.22375   0.15107  1.61
policonsumo2.lag              -0.08624   0.91737  0.37665   0.22091 -0.39
edad_al_ing_1                  0.09182   1.09616  0.02331   0.02261  4.06
ano_nac_corr                   0.08735   1.09128  0.02300   0.02202  3.97
susinidum_oh                  -0.18020   0.83511  0.18632   0.18875 -0.95
susinidum_coc                 -0.00580   0.99422  0.23382   0.25265 -0.02
susinidum_pbc                  0.09401   1.09857  0.22961   0.22434  0.42
susinidum_mar                 -0.06294   0.93900  0.19177   0.19659 -0.32
psycom_dum_with                0.11019   1.11649  0.07414   0.07566  1.46
psycom_dum_study               0.07103   1.07361  0.10012   0.09529  0.75
freq_cons_dum_5day             0.03120   1.03170  0.06792   0.06685  0.47
cond_oc_dum_2inact             0.08752   1.09146  0.10398   0.10352  0.85
cond_oc_dum_3unemp            -0.00759   0.99244  0.07182   0.07088 -0.11
susprindum_oh                 -0.33860   0.71277  0.30834   0.26303 -1.29
susprindum_coc                -0.49853   0.60742  0.31331   0.26966 -1.85
susprindum_pbc                -0.35718   0.69965  0.30406   0.26011 -1.37
susprindum_mar                -0.37937   0.68429  0.32550   0.29400 -1.29
                              Pr(>|z|)    
tr_outcome.lag                   0.547    
less_90d_tr1.lag                 0.829    
log_dias_treat_imp_sin_na.lag    0.648    
comp_bpsc_y3_severe.lag          0.107    
policonsumo2.lag                 0.696    
edad_al_ing_1                  4.9e-05 ***
ano_nac_corr                   7.3e-05 ***
susinidum_oh                     0.340    
susinidum_coc                    0.982    
susinidum_pbc                    0.675    
susinidum_mar                    0.749    
psycom_dum_with                  0.145    
psycom_dum_study                 0.456    
freq_cons_dum_5day               0.641    
cond_oc_dum_2inact               0.398    
cond_oc_dum_3unemp               0.915    
susprindum_oh                    0.198    
susprindum_coc                   0.064 .  
susprindum_pbc                   0.170    
susprindum_mar                   0.197    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    1.155      0.866     0.723      1.84
less_90d_tr1.lag                  1.042      0.960     0.719      1.51
log_dias_treat_imp_sin_na.lag     0.937      1.067     0.709      1.24
comp_bpsc_y3_severe.lag           1.276      0.784     0.949      1.72
policonsumo2.lag                  0.917      1.090     0.595      1.41
edad_al_ing_1                     1.096      0.912     1.049      1.15
ano_nac_corr                      1.091      0.916     1.045      1.14
susinidum_oh                      0.835      1.197     0.577      1.21
susinidum_coc                     0.994      1.006     0.606      1.63
susinidum_pbc                     1.099      0.910     0.708      1.71
susinidum_mar                     0.939      1.065     0.639      1.38
psycom_dum_with                   1.116      0.896     0.963      1.29
psycom_dum_study                  1.074      0.931     0.891      1.29
freq_cons_dum_5day                1.032      0.969     0.905      1.18
cond_oc_dum_2inact                1.091      0.916     0.891      1.34
cond_oc_dum_3unemp                0.992      1.008     0.864      1.14
susprindum_oh                     0.713      1.403     0.426      1.19
susprindum_coc                    0.607      1.646     0.358      1.03
susprindum_pbc                    0.700      1.429     0.420      1.16
susprindum_mar                    0.684      1.461     0.385      1.22

Concordance= 0.563  (se = 0.01 )
Likelihood ratio test= 35.3  on 20 df,   p=0.02
Wald test            = 43.2  on 20 df,   p=0.002
Score (logrank) test = 35  on 20 df,   p=0.02,   Robust = 41.4  p=0.003

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.592   0.927   1.061   1.091   1.220   2.472 
[1] "Modality: basic ambulatory\nTime interval: (50,70]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 975, number of events= 975 
   (912 observations deleted due to missingness)

                                 coef exp(coef) se(coef) robust se     z
tr_outcome.lag                -0.0925    0.9117   0.3173    0.2442 -0.38
less_90d_tr1.lag               0.0706    1.0731   0.2769    0.1839  0.38
log_dias_treat_imp_sin_na.lag  0.0404    1.0412   0.1784    0.1286  0.31
comp_bpsc_y3_severe.lag        0.4059    1.5007   0.2303    0.1791  2.27
policonsumo2.lag               0.1346    1.1441   0.2812    0.2287  0.59
edad_al_ing_1                  0.1269    1.1353   0.0210    0.0197  6.45
ano_nac_corr                   0.1320    1.1412   0.0210    0.0198  6.67
susinidum_oh                  -0.0679    0.9344   0.1746    0.2017 -0.34
susinidum_coc                 -0.3007    0.7403   0.2407    0.2727 -1.10
susinidum_pbc                 -0.1127    0.8935   0.2201    0.2293 -0.49
susinidum_mar                 -0.0690    0.9334   0.1813    0.2049 -0.34
psycom_dum_with               -0.1255    0.8820   0.0738    0.0725 -1.73
psycom_dum_study              -0.0329    0.9677   0.0939    0.0917 -0.36
freq_cons_dum_5day             0.0826    1.0861   0.0754    0.0753  1.10
cond_oc_dum_2inact             0.1227    1.1306   0.1038    0.1018  1.21
cond_oc_dum_3unemp             0.0404    1.0412   0.0784    0.0754  0.54
susprindum_oh                  0.2790    1.3218   0.2321    0.2324  1.20
susprindum_coc                 0.2343    1.2640   0.2361    0.2345  1.00
susprindum_pbc                 0.1675    1.1824   0.2326    0.2319  0.72
susprindum_mar                 0.0313    1.0318   0.2554    0.2589  0.12
                              Pr(>|z|)    
tr_outcome.lag                   0.705    
less_90d_tr1.lag                 0.701    
log_dias_treat_imp_sin_na.lag    0.753    
comp_bpsc_y3_severe.lag          0.023 *  
policonsumo2.lag                 0.556    
edad_al_ing_1                  1.1e-10 ***
ano_nac_corr                   2.5e-11 ***
susinidum_oh                     0.737    
susinidum_coc                    0.270    
susinidum_pbc                    0.623    
susinidum_mar                    0.736    
psycom_dum_with                  0.083 .  
psycom_dum_study                 0.720    
freq_cons_dum_5day               0.273    
cond_oc_dum_2inact               0.228    
cond_oc_dum_3unemp               0.592    
susprindum_oh                    0.230    
susprindum_coc                   0.318    
susprindum_pbc                   0.470    
susprindum_mar                   0.904    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    0.912      1.097     0.565      1.47
less_90d_tr1.lag                  1.073      0.932     0.748      1.54
log_dias_treat_imp_sin_na.lag     1.041      0.960     0.809      1.34
comp_bpsc_y3_severe.lag           1.501      0.666     1.056      2.13
policonsumo2.lag                  1.144      0.874     0.731      1.79
edad_al_ing_1                     1.135      0.881     1.092      1.18
ano_nac_corr                      1.141      0.876     1.098      1.19
susinidum_oh                      0.934      1.070     0.629      1.39
susinidum_coc                     0.740      1.351     0.434      1.26
susinidum_pbc                     0.893      1.119     0.570      1.40
susinidum_mar                     0.933      1.071     0.625      1.39
psycom_dum_with                   0.882      1.134     0.765      1.02
psycom_dum_study                  0.968      1.033     0.808      1.16
freq_cons_dum_5day                1.086      0.921     0.937      1.26
cond_oc_dum_2inact                1.131      0.884     0.926      1.38
cond_oc_dum_3unemp                1.041      0.960     0.898      1.21
susprindum_oh                     1.322      0.757     0.838      2.08
susprindum_coc                    1.264      0.791     0.798      2.00
susprindum_pbc                    1.182      0.846     0.750      1.86
susprindum_mar                    1.032      0.969     0.621      1.71

Concordance= 0.577  (se = 0.01 )
Likelihood ratio test= 64.2  on 20 df,   p=2e-06
Wald test            = 66.3  on 20 df,   p=7e-07
Score (logrank) test = 63.6  on 20 df,   p=2e-06,   Robust = 69.5  p=2e-07

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.290   0.436   0.539   0.559   0.655   1.455 
[1] "Modality: GP residential\nTime interval: (50,70]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 392, number of events= 392 
   (361 observations deleted due to missingness)

                                  coef exp(coef) se(coef) robust se      z
tr_outcome.lag                -0.98955   0.37174  0.45799   0.31909  -3.10
less_90d_tr1.lag               0.83212   2.29819  0.54609   0.48760   1.71
log_dias_treat_imp_sin_na.lag  0.01531   1.01542  0.43790   0.46770   0.03
comp_bpsc_y3_severe.lag       -0.20089   0.81800  0.49058   0.31748  -0.63
policonsumo2.lag               0.18496   1.20317  0.60446   0.43474   0.43
edad_al_ing_1                  0.09172   1.09606  0.03762   0.03716   2.47
ano_nac_corr                   0.10229   1.10770  0.03758   0.03768   2.71
susinidum_oh                   0.29856   1.34792  0.28694   0.25281   1.18
susinidum_coc                  0.23353   1.26305  0.37920   0.32510   0.72
susinidum_pbc                  0.11437   1.12116  0.31636   0.29190   0.39
susinidum_mar                  0.05158   1.05293  0.28712   0.25138   0.21
psycom_dum_with               -0.20375   0.81567  0.12050   0.11684  -1.74
psycom_dum_study              -0.18978   0.82714  0.17528   0.16004  -1.19
freq_cons_dum_5day            -0.10802   0.89761  0.11907   0.12143  -0.89
cond_oc_dum_2inact             0.00394   1.00395  0.20053   0.19286   0.02
cond_oc_dum_3unemp            -0.10783   0.89778  0.15618   0.15639  -0.69
susprindum_oh                 -1.69453   0.18369  0.73308   0.20093  -8.43
susprindum_coc                -2.05429   0.12818  0.74059   0.22332  -9.20
susprindum_pbc                -1.88226   0.15225  0.72721   0.15822 -11.90
susprindum_mar                -1.96932   0.13955  0.80891   0.42217  -4.66
                              Pr(>|z|)    
tr_outcome.lag                  0.0019 ** 
less_90d_tr1.lag                0.0879 .  
log_dias_treat_imp_sin_na.lag   0.9739    
comp_bpsc_y3_severe.lag         0.5269    
policonsumo2.lag                0.6705    
edad_al_ing_1                   0.0136 *  
ano_nac_corr                    0.0066 ** 
susinidum_oh                    0.2376    
susinidum_coc                   0.4725    
susinidum_pbc                   0.6952    
susinidum_mar                   0.8374    
psycom_dum_with                 0.0812 .  
psycom_dum_study                0.2357    
freq_cons_dum_5day              0.3737    
cond_oc_dum_2inact              0.9837    
cond_oc_dum_3unemp              0.4905    
susprindum_oh                  < 2e-16 ***
susprindum_coc                 < 2e-16 ***
susprindum_pbc                 < 2e-16 ***
susprindum_mar                 3.1e-06 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    0.372      2.690    0.1989     0.695
less_90d_tr1.lag                  2.298      0.435    0.8838     5.976
log_dias_treat_imp_sin_na.lag     1.015      0.985    0.4060     2.540
comp_bpsc_y3_severe.lag           0.818      1.222    0.4391     1.524
policonsumo2.lag                  1.203      0.831    0.5132     2.821
edad_al_ing_1                     1.096      0.912    1.0191     1.179
ano_nac_corr                      1.108      0.903    1.0288     1.193
susinidum_oh                      1.348      0.742    0.8212     2.212
susinidum_coc                     1.263      0.792    0.6679     2.389
susinidum_pbc                     1.121      0.892    0.6327     1.987
susinidum_mar                     1.053      0.950    0.6433     1.723
psycom_dum_with                   0.816      1.226    0.6487     1.026
psycom_dum_study                  0.827      1.209    0.6044     1.132
freq_cons_dum_5day                0.898      1.114    0.7075     1.139
cond_oc_dum_2inact                1.004      0.996    0.6879     1.465
cond_oc_dum_3unemp                0.898      1.114    0.6608     1.220
susprindum_oh                     0.184      5.444    0.1239     0.272
susprindum_coc                    0.128      7.801    0.0827     0.199
susprindum_pbc                    0.152      6.568    0.1117     0.208
susprindum_mar                    0.140      7.166    0.0610     0.319

Concordance= 0.575  (se = 0.017 )
Likelihood ratio test= 32.7  on 20 df,   p=0.04
Wald test            = 228  on 20 df,   p=<2e-16
Score (logrank) test = 34.1  on 20 df,   p=0.03,   Robust = 31  p=0.06

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   1.64    7.43    8.78    9.27   10.39   28.37 
[1] "Modality: WO intensive ambulatory\nTime interval: (50,70]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 147, number of events= 147 
   (135 observations deleted due to missingness)

                                  coef exp(coef) se(coef) robust se     z
tr_outcome.lag                 2.24572   9.44720  0.97612   0.84362  2.66
less_90d_tr1.lag              -1.48459   0.22660  0.79164   0.59000 -2.52
log_dias_treat_imp_sin_na.lag -1.00834   0.36482  0.56973   0.38515 -2.62
comp_bpsc_y3_severe.lag        0.80894   2.24553  0.83762   0.33900  2.39
policonsumo2.lag              -1.70058   0.18258  1.35294   0.91105 -1.87
edad_al_ing_1                  0.12532   1.13351  0.05847   0.05535  2.26
ano_nac_corr                   0.11522   1.12212  0.05606   0.05292  2.18
susinidum_oh                  -0.39560   0.67327  0.67106   0.38664 -1.02
susinidum_coc                  0.06768   1.07002  0.86967   0.72300  0.09
susinidum_pbc                 -0.74257   0.47589  0.71302   0.48039 -1.55
susinidum_mar                 -0.35549   0.70083  0.68367   0.43209 -0.82
psycom_dum_with                0.00121   1.00121  0.22120   0.23127  0.01
psycom_dum_study               0.25008   1.28413  0.31634   0.32202  0.78
freq_cons_dum_5day            -0.07136   0.93113  0.19453   0.18559 -0.38
cond_oc_dum_2inact             0.13726   1.14713  0.24366   0.26884  0.51
cond_oc_dum_3unemp            -0.03216   0.96835  0.25678   0.28903 -0.11
susprindum_oh                 -0.51243   0.59904  0.78028   0.37638 -1.36
susprindum_coc                -0.79785   0.45029  0.81330   0.53602 -1.49
susprindum_pbc                -0.39730   0.67213  0.77006   0.40286 -0.99
susprindum_mar                -1.05249   0.34907  0.81911   0.46387 -2.27
                              Pr(>|z|)   
tr_outcome.lag                  0.0078 **
less_90d_tr1.lag                0.0119 * 
log_dias_treat_imp_sin_na.lag   0.0088 **
comp_bpsc_y3_severe.lag         0.0170 * 
policonsumo2.lag                0.0620 . 
edad_al_ing_1                   0.0236 * 
ano_nac_corr                    0.0295 * 
susinidum_oh                    0.3062   
susinidum_coc                   0.9254   
susinidum_pbc                   0.1222   
susinidum_mar                   0.4107   
psycom_dum_with                 0.9958   
psycom_dum_study                0.4374   
freq_cons_dum_5day              0.7006   
cond_oc_dum_2inact              0.6097   
cond_oc_dum_3unemp              0.9114   
susprindum_oh                   0.1734   
susprindum_coc                  0.1366   
susprindum_pbc                  0.3240   
susprindum_mar                  0.0233 * 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    9.447      0.106    1.8081    49.362
less_90d_tr1.lag                  0.227      4.413    0.0713     0.720
log_dias_treat_imp_sin_na.lag     0.365      2.741    0.1715     0.776
comp_bpsc_y3_severe.lag           2.246      0.445    1.1555     4.364
policonsumo2.lag                  0.183      5.477    0.0306     1.089
edad_al_ing_1                     1.134      0.882    1.0170     1.263
ano_nac_corr                      1.122      0.891    1.0116     1.245
susinidum_oh                      0.673      1.485    0.3156     1.436
susinidum_coc                     1.070      0.935    0.2594     4.414
susinidum_pbc                     0.476      2.101    0.1856     1.220
susinidum_mar                     0.701      1.427    0.3005     1.635
psycom_dum_with                   1.001      0.999    0.6363     1.575
psycom_dum_study                  1.284      0.779    0.6831     2.414
freq_cons_dum_5day                0.931      1.074    0.6472     1.340
cond_oc_dum_2inact                1.147      0.872    0.6773     1.943
cond_oc_dum_3unemp                0.968      1.033    0.5496     1.706
susprindum_oh                     0.599      1.669    0.2865     1.253
susprindum_coc                    0.450      2.221    0.1575     1.288
susprindum_pbc                    0.672      1.488    0.3052     1.480
susprindum_mar                    0.349      2.865    0.1406     0.866

Concordance= 0.614  (se = 0.025 )
Likelihood ratio test= 29.4  on 20 df,   p=0.08
Wald test            = 77.1  on 20 df,   p=1e-08
Score (logrank) test = 25.8  on 20 df,   p=0.2,   Robust = 28.5  p=0.1

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
    0.0     2.0     2.6     3.2     3.3    42.1 
[1] "Modality: WO residential\nTime interval: (70,135]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 140, number of events= 140 
   (114 observations deleted due to missingness)

                                 coef exp(coef) se(coef) robust se     z
tr_outcome.lag                 0.2111    1.2351   0.5133    0.3758  0.56
less_90d_tr1.lag               0.1271    1.1356   0.4777    0.3623  0.35
log_dias_treat_imp_sin_na.lag  0.0950    1.0997   0.2702    0.1604  0.59
comp_bpsc_y3_severe.lag       -0.0265    0.9738   0.5683    0.4003 -0.07
policonsumo2.lag               0.4960    1.6421   0.7126    0.5910  0.84
edad_al_ing_1                  0.7531    2.1235   0.1295    0.1259  5.98
ano_nac_corr                   0.7581    2.1342   0.1267    0.1213  6.25
susinidum_oh                  -0.1409    0.8686   0.7396    0.6574 -0.21
susinidum_coc                  0.2827    1.3267   0.9117    0.8001  0.35
susinidum_pbc                  0.0692    1.0716   0.7778    0.7062  0.10
susinidum_mar                 -0.4544    0.6348   0.7461    0.6705 -0.68
psycom_dum_with                0.2074    1.2304   0.2479    0.2219  0.93
psycom_dum_study               0.3116    1.3656   0.3006    0.2733  1.14
freq_cons_dum_5day            -0.1393    0.8700   0.2268    0.2203 -0.63
cond_oc_dum_2inact             0.3615    1.4355   0.3814    0.2760  1.31
cond_oc_dum_3unemp             0.3359    1.3992   0.3732    0.2670  1.26
susprindum_oh                 -0.8623    0.4222   0.6834    0.5388 -1.60
susprindum_coc                -0.7442    0.4751   0.6847    0.5885 -1.26
susprindum_pbc                -0.8763    0.4163   0.6689    0.5671 -1.55
susprindum_mar                -0.1224    0.8848   0.8132    0.7029 -0.17
                              Pr(>|z|)    
tr_outcome.lag                    0.57    
less_90d_tr1.lag                  0.73    
log_dias_treat_imp_sin_na.lag     0.55    
comp_bpsc_y3_severe.lag           0.95    
policonsumo2.lag                  0.40    
edad_al_ing_1                  2.2e-09 ***
ano_nac_corr                   4.1e-10 ***
susinidum_oh                      0.83    
susinidum_coc                     0.72    
susinidum_pbc                     0.92    
susinidum_mar                     0.50    
psycom_dum_with                   0.35    
psycom_dum_study                  0.25    
freq_cons_dum_5day                0.53    
cond_oc_dum_2inact                0.19    
cond_oc_dum_3unemp                0.21    
susprindum_oh                     0.11    
susprindum_coc                    0.21    
susprindum_pbc                    0.12    
susprindum_mar                    0.86    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    1.235      0.810     0.591      2.58
less_90d_tr1.lag                  1.136      0.881     0.558      2.31
log_dias_treat_imp_sin_na.lag     1.100      0.909     0.803      1.51
comp_bpsc_y3_severe.lag           0.974      1.027     0.444      2.13
policonsumo2.lag                  1.642      0.609     0.516      5.23
edad_al_ing_1                     2.123      0.471     1.659      2.72
ano_nac_corr                      2.134      0.469     1.683      2.71
susinidum_oh                      0.869      1.151     0.239      3.15
susinidum_coc                     1.327      0.754     0.277      6.37
susinidum_pbc                     1.072      0.933     0.268      4.28
susinidum_mar                     0.635      1.575     0.171      2.36
psycom_dum_with                   1.230      0.813     0.797      1.90
psycom_dum_study                  1.366      0.732     0.799      2.33
freq_cons_dum_5day                0.870      1.149     0.565      1.34
cond_oc_dum_2inact                1.436      0.697     0.836      2.47
cond_oc_dum_3unemp                1.399      0.715     0.829      2.36
susprindum_oh                     0.422      2.368     0.147      1.21
susprindum_coc                    0.475      2.105     0.150      1.51
susprindum_pbc                    0.416      2.402     0.137      1.27
susprindum_mar                    0.885      1.130     0.223      3.51

Concordance= 0.692  (se = 0.021 )
Likelihood ratio test= 60.2  on 20 df,   p=7e-06
Wald test            = 101  on 20 df,   p=8e-13
Score (logrank) test = 66.8  on 20 df,   p=6e-07,   Robust = 63.5  p=2e-06

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.11    0.47    1.03    1.17    1.55    4.14 
[1] "Modality: GP intensive ambulatory\nTime interval: (70,135]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 747, number of events= 747 
   (659 observations deleted due to missingness)

                                  coef exp(coef) se(coef) robust se     z
tr_outcome.lag                -0.10065   0.90425  0.27513   0.19992 -0.50
less_90d_tr1.lag               0.22444   1.25162  0.28593   0.20305  1.11
log_dias_treat_imp_sin_na.lag  0.08647   1.09032  0.20200   0.14347  0.60
comp_bpsc_y3_severe.lag        0.08014   1.08344  0.21268   0.15931  0.50
policonsumo2.lag               0.28875   1.33476  0.26990   0.18411  1.57
edad_al_ing_1                  0.53755   1.71182  0.03877   0.03623 14.84
ano_nac_corr                   0.53650   1.71001  0.03879   0.03598 14.91
susinidum_oh                  -0.08569   0.91788  0.23039   0.22759 -0.38
susinidum_coc                  0.01008   1.01013  0.29231   0.28871  0.03
susinidum_pbc                  0.00295   1.00295  0.28632   0.27217  0.01
susinidum_mar                  0.01616   1.01629  0.23648   0.23409  0.07
psycom_dum_with               -0.11452   0.89179  0.08329   0.08055 -1.42
psycom_dum_study              -0.01216   0.98792  0.11479   0.10612 -0.11
freq_cons_dum_5day            -0.01605   0.98408  0.07879   0.07317 -0.22
cond_oc_dum_2inact             0.07352   1.07629  0.12276   0.11230  0.65
cond_oc_dum_3unemp             0.02951   1.02995  0.08323   0.07784  0.38
susprindum_oh                 -0.06372   0.93827  0.33425   0.18919 -0.34
susprindum_coc                -0.06906   0.93327  0.33636   0.19413 -0.36
susprindum_pbc                -0.11980   0.88709  0.32915   0.17929 -0.67
susprindum_mar                -0.04056   0.96025  0.35340   0.22979 -0.18
                              Pr(>|z|)    
tr_outcome.lag                    0.61    
less_90d_tr1.lag                  0.27    
log_dias_treat_imp_sin_na.lag     0.55    
comp_bpsc_y3_severe.lag           0.61    
policonsumo2.lag                  0.12    
edad_al_ing_1                   <2e-16 ***
ano_nac_corr                    <2e-16 ***
susinidum_oh                      0.71    
susinidum_coc                     0.97    
susinidum_pbc                     0.99    
susinidum_mar                     0.94    
psycom_dum_with                   0.16    
psycom_dum_study                  0.91    
freq_cons_dum_5day                0.83    
cond_oc_dum_2inact                0.51    
cond_oc_dum_3unemp                0.70    
susprindum_oh                     0.74    
susprindum_coc                    0.72    
susprindum_pbc                    0.50    
susprindum_mar                    0.86    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    0.904      1.106     0.611      1.34
less_90d_tr1.lag                  1.252      0.799     0.841      1.86
log_dias_treat_imp_sin_na.lag     1.090      0.917     0.823      1.44
comp_bpsc_y3_severe.lag           1.083      0.923     0.793      1.48
policonsumo2.lag                  1.335      0.749     0.930      1.91
edad_al_ing_1                     1.712      0.584     1.594      1.84
ano_nac_corr                      1.710      0.585     1.594      1.83
susinidum_oh                      0.918      1.089     0.588      1.43
susinidum_coc                     1.010      0.990     0.574      1.78
susinidum_pbc                     1.003      0.997     0.588      1.71
susinidum_mar                     1.016      0.984     0.642      1.61
psycom_dum_with                   0.892      1.121     0.762      1.04
psycom_dum_study                  0.988      1.012     0.802      1.22
freq_cons_dum_5day                0.984      1.016     0.853      1.14
cond_oc_dum_2inact                1.076      0.929     0.864      1.34
cond_oc_dum_3unemp                1.030      0.971     0.884      1.20
susprindum_oh                     0.938      1.066     0.648      1.36
susprindum_coc                    0.933      1.072     0.638      1.37
susprindum_pbc                    0.887      1.127     0.624      1.26
susprindum_mar                    0.960      1.041     0.612      1.51

Concordance= 0.643  (se = 0.011 )
Likelihood ratio test= 214  on 20 df,   p=<2e-16
Wald test            = 271  on 20 df,   p=<2e-16
Score (logrank) test = 217  on 20 df,   p=<2e-16,   Robust = 197  p=<2e-16

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.12    0.48    0.82    0.93    1.24    3.75 
[1] "Modality: basic ambulatory\nTime interval: (70,135]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 699, number of events= 699 
   (617 observations deleted due to missingness)

                                 coef exp(coef) se(coef) robust se     z
tr_outcome.lag                -0.1665    0.8466   0.2975    0.2632 -0.63
less_90d_tr1.lag              -0.4788    0.6195   0.2536    0.2275 -2.10
log_dias_treat_imp_sin_na.lag -0.3879    0.6785   0.1119    0.0973 -3.99
comp_bpsc_y3_severe.lag        0.3991    1.4905   0.2041    0.1726  2.31
policonsumo2.lag              -0.0987    0.9060   0.2908    0.2904 -0.34
edad_al_ing_1                  0.5149    1.6735   0.0418    0.0407 12.64
ano_nac_corr                   0.5131    1.6704   0.0414    0.0406 12.65
susinidum_oh                  -0.1703    0.8434   0.2424    0.2253 -0.76
susinidum_coc                 -0.3957    0.6732   0.2808    0.2530 -1.56
susinidum_pbc                 -0.1599    0.8523   0.2782    0.2542 -0.63
susinidum_mar                 -0.1832    0.8326   0.2505    0.2308 -0.79
psycom_dum_with                0.0881    1.0921   0.0869    0.0819  1.08
psycom_dum_study              -0.0535    0.9479   0.1099    0.0978 -0.55
freq_cons_dum_5day            -0.0173    0.9829   0.0880    0.0812 -0.21
cond_oc_dum_2inact             0.1352    1.1448   0.1208    0.1225  1.10
cond_oc_dum_3unemp            -0.1115    0.8945   0.0954    0.0841 -1.32
susprindum_oh                 -0.2414    0.7855   0.3180    0.2378 -1.02
susprindum_coc                -0.1284    0.8795   0.3166    0.2318 -0.55
susprindum_pbc                -0.1876    0.8290   0.3126    0.2252 -0.83
susprindum_mar                -0.0853    0.9183   0.3332    0.2448 -0.35
                              Pr(>|z|)    
tr_outcome.lag                   0.527    
less_90d_tr1.lag                 0.035 *  
log_dias_treat_imp_sin_na.lag  6.7e-05 ***
comp_bpsc_y3_severe.lag          0.021 *  
policonsumo2.lag                 0.734    
edad_al_ing_1                  < 2e-16 ***
ano_nac_corr                   < 2e-16 ***
susinidum_oh                     0.450    
susinidum_coc                    0.118    
susinidum_pbc                    0.529    
susinidum_mar                    0.427    
psycom_dum_with                  0.282    
psycom_dum_study                 0.585    
freq_cons_dum_5day               0.832    
cond_oc_dum_2inact               0.270    
cond_oc_dum_3unemp               0.185    
susprindum_oh                    0.310    
susprindum_coc                   0.580    
susprindum_pbc                   0.405    
susprindum_mar                   0.728    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    0.847      1.181     0.505     1.418
less_90d_tr1.lag                  0.620      1.614     0.397     0.968
log_dias_treat_imp_sin_na.lag     0.679      1.474     0.561     0.821
comp_bpsc_y3_severe.lag           1.491      0.671     1.063     2.091
policonsumo2.lag                  0.906      1.104     0.513     1.601
edad_al_ing_1                     1.674      0.598     1.545     1.813
ano_nac_corr                      1.670      0.599     1.543     1.809
susinidum_oh                      0.843      1.186     0.542     1.312
susinidum_coc                     0.673      1.485     0.410     1.105
susinidum_pbc                     0.852      1.173     0.518     1.403
susinidum_mar                     0.833      1.201     0.530     1.309
psycom_dum_with                   1.092      0.916     0.930     1.282
psycom_dum_study                  0.948      1.055     0.783     1.148
freq_cons_dum_5day                0.983      1.017     0.838     1.152
cond_oc_dum_2inact                1.145      0.874     0.900     1.455
cond_oc_dum_3unemp                0.895      1.118     0.759     1.055
susprindum_oh                     0.785      1.273     0.493     1.252
susprindum_coc                    0.880      1.137     0.558     1.385
susprindum_pbc                    0.829      1.206     0.533     1.289
susprindum_mar                    0.918      1.089     0.568     1.484

Concordance= 0.628  (se = 0.012 )
Likelihood ratio test= 173  on 20 df,   p=<2e-16
Wald test            = 198  on 20 df,   p=<2e-16
Score (logrank) test = 174  on 20 df,   p=<2e-16,   Robust = 151  p=<2e-16

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.37    1.31    2.03    2.33    3.02   12.32 
[1] "Modality: GP residential\nTime interval: (70,135]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 325, number of events= 325 
   (273 observations deleted due to missingness)

                                 coef exp(coef) se(coef) robust se     z
tr_outcome.lag                -0.1283    0.8796   0.3809    0.2124 -0.60
less_90d_tr1.lag               0.2810    1.3244   0.3371    0.2433  1.15
log_dias_treat_imp_sin_na.lag -0.0898    0.9141   0.2532    0.1974 -0.45
comp_bpsc_y3_severe.lag        0.3132    1.3678   0.3254    0.1937  1.62
policonsumo2.lag              -0.2731    0.7610   0.3553    0.2857 -0.96
edad_al_ing_1                  0.6022    1.8261   0.0657    0.0567 10.61
ano_nac_corr                   0.6002    1.8225   0.0658    0.0564 10.64
susinidum_oh                  -0.5709    0.5650   0.3978    0.2116 -2.70
susinidum_coc                 -0.4843    0.6162   0.4715    0.2666 -1.82
susinidum_pbc                 -0.6679    0.5128   0.4202    0.2403 -2.78
susinidum_mar                 -0.7051    0.4941   0.4015    0.2205 -3.20
psycom_dum_with                0.0729    1.0756   0.1290    0.1183  0.62
psycom_dum_study               0.1026    1.1081   0.1852    0.1607  0.64
freq_cons_dum_5day            -0.1321    0.8762   0.1262    0.1142 -1.16
cond_oc_dum_2inact            -0.3295    0.7193   0.2323    0.1969 -1.67
cond_oc_dum_3unemp            -0.3918    0.6758   0.1649    0.1471 -2.66
susprindum_oh                  0.1714    1.1869   0.7397    0.3254  0.53
susprindum_coc                 0.1972    1.2179   0.7364    0.3188  0.62
susprindum_pbc                 0.2918    1.3389   0.7228    0.2736  1.07
susprindum_mar                 0.2866    1.3319   0.8015    0.3709  0.77
                              Pr(>|z|)    
tr_outcome.lag                  0.5457    
less_90d_tr1.lag                0.2483    
log_dias_treat_imp_sin_na.lag   0.6492    
comp_bpsc_y3_severe.lag         0.1059    
policonsumo2.lag                0.3391    
edad_al_ing_1                   <2e-16 ***
ano_nac_corr                    <2e-16 ***
susinidum_oh                    0.0070 ** 
susinidum_coc                   0.0693 .  
susinidum_pbc                   0.0054 ** 
susinidum_mar                   0.0014 ** 
psycom_dum_with                 0.5378    
psycom_dum_study                0.5231    
freq_cons_dum_5day              0.2475    
cond_oc_dum_2inact              0.0943 .  
cond_oc_dum_3unemp              0.0077 ** 
susprindum_oh                   0.5985    
susprindum_coc                  0.5363    
susprindum_pbc                  0.2861    
susprindum_mar                  0.4397    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    0.880      1.137     0.580     1.334
less_90d_tr1.lag                  1.324      0.755     0.822     2.134
log_dias_treat_imp_sin_na.lag     0.914      1.094     0.621     1.346
comp_bpsc_y3_severe.lag           1.368      0.731     0.936     1.999
policonsumo2.lag                  0.761      1.314     0.435     1.332
edad_al_ing_1                     1.826      0.548     1.634     2.041
ano_nac_corr                      1.822      0.549     1.632     2.036
susinidum_oh                      0.565      1.770     0.373     0.855
susinidum_coc                     0.616      1.623     0.365     1.039
susinidum_pbc                     0.513      1.950     0.320     0.821
susinidum_mar                     0.494      2.024     0.321     0.761
psycom_dum_with                   1.076      0.930     0.853     1.356
psycom_dum_study                  1.108      0.902     0.809     1.518
freq_cons_dum_5day                0.876      1.141     0.700     1.096
cond_oc_dum_2inact                0.719      1.390     0.489     1.058
cond_oc_dum_3unemp                0.676      1.480     0.507     0.902
susprindum_oh                     1.187      0.843     0.627     2.246
susprindum_coc                    1.218      0.821     0.652     2.275
susprindum_pbc                    1.339      0.747     0.783     2.289
susprindum_mar                    1.332      0.751     0.644     2.755

Concordance= 0.652  (se = 0.015 )
Likelihood ratio test= 96  on 20 df,   p=7e-12
Wald test            = 144  on 20 df,   p=<2e-16
Score (logrank) test = 99.3  on 20 df,   p=2e-12,   Robust = 100  p=1e-12

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.21    1.13    1.84    2.11    2.79    6.63 
[1] "Modality: WO intensive ambulatory\nTime interval: (70,135]"
Call:
coxph(formula = Surv(time.lag, time, event) ~ tr_outcome.lag + 
    less_90d_tr1.lag + log_dias_treat_imp_sin_na.lag + comp_bpsc_y3_severe.lag + 
    policonsumo2.lag + edad_al_ing_1 + ano_nac_corr + susinidum_oh + 
    susinidum_coc + susinidum_pbc + susinidum_mar + psycom_dum_with + 
    psycom_dum_study + freq_cons_dum_5day + cond_oc_dum_2inact + 
    cond_oc_dum_3unemp + susprindum_oh + susprindum_coc + susprindum_pbc + 
    susprindum_mar, data = datacox, cluster = id)

  n= 109, number of events= 109 
   (89 observations deleted due to missingness)

                                 coef exp(coef) se(coef) robust se     z
tr_outcome.lag                 0.6069    1.8347   0.6968    0.5730  1.06
less_90d_tr1.lag               0.9729    2.6457   0.7169    0.4316  2.25
log_dias_treat_imp_sin_na.lag -0.0350    0.9656   0.2175    0.0989 -0.35
comp_bpsc_y3_severe.lag       -0.5660    0.5678   0.6461    0.4651 -1.22
policonsumo2.lag              -1.1665    0.3114   0.7091    0.4960 -2.35
edad_al_ing_1                  0.7256    2.0660   0.1272    0.1194  6.08
ano_nac_corr                   0.7006    2.0149   0.1234    0.1180  5.94
susinidum_oh                   0.1262    1.1345   1.1294    0.4389  0.29
susinidum_coc                 -1.3507    0.2591   1.5526    0.5259 -2.57
susinidum_pbc                  0.6761    1.9662   1.1723    0.5557  1.22
susinidum_mar                  0.0301    1.0305   1.1478    0.5383  0.06
psycom_dum_with               -0.0459    0.9552   0.2525    0.2479 -0.19
psycom_dum_study               0.3213    1.3789   0.3185    0.2964  1.08
freq_cons_dum_5day             0.3997    1.4914   0.2730    0.2428  1.65
cond_oc_dum_2inact             0.3015    1.3519   0.3518    0.3294  0.92
cond_oc_dum_3unemp             0.1458    1.1570   0.3493    0.3020  0.48
susprindum_oh                  0.7995    2.2245   0.8200    0.5875  1.36
susprindum_coc                 0.6280    1.8739   0.8374    0.5909  1.06
susprindum_pbc                 0.7274    2.0697   0.7852    0.5697  1.28
susprindum_mar                 1.1757    3.2405   0.9061    0.6070  1.94
                              Pr(>|z|)    
tr_outcome.lag                   0.289    
less_90d_tr1.lag                 0.024 *  
log_dias_treat_imp_sin_na.lag    0.723    
comp_bpsc_y3_severe.lag          0.224    
policonsumo2.lag                 0.019 *  
edad_al_ing_1                  1.2e-09 ***
ano_nac_corr                   2.9e-09 ***
susinidum_oh                     0.774    
susinidum_coc                    0.010 *  
susinidum_pbc                    0.224    
susinidum_mar                    0.955    
psycom_dum_with                  0.853    
psycom_dum_study                 0.278    
freq_cons_dum_5day               0.100 .  
cond_oc_dum_2inact               0.360    
cond_oc_dum_3unemp               0.629    
susprindum_oh                    0.174    
susprindum_coc                   0.288    
susprindum_pbc                   0.202    
susprindum_mar                   0.053 .  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                              exp(coef) exp(-coef) lower .95 upper .95
tr_outcome.lag                    1.835      0.545    0.5968     5.640
less_90d_tr1.lag                  2.646      0.378    1.1354     6.165
log_dias_treat_imp_sin_na.lag     0.966      1.036    0.7954     1.172
comp_bpsc_y3_severe.lag           0.568      1.761    0.2282     1.413
policonsumo2.lag                  0.311      3.211    0.1178     0.823
edad_al_ing_1                     2.066      0.484    1.6348     2.611
ano_nac_corr                      2.015      0.496    1.5988     2.539
susinidum_oh                      1.135      0.881    0.4800     2.682
susinidum_coc                     0.259      3.860    0.0924     0.726
susinidum_pbc                     1.966      0.509    0.6616     5.844
susinidum_mar                     1.031      0.970    0.3588     2.960
psycom_dum_with                   0.955      1.047    0.5876     1.553
psycom_dum_study                  1.379      0.725    0.7714     2.465
freq_cons_dum_5day                1.491      0.671    0.9267     2.400
cond_oc_dum_2inact                1.352      0.740    0.7089     2.578
cond_oc_dum_3unemp                1.157      0.864    0.6401     2.091
susprindum_oh                     2.225      0.450    0.7033     7.036
susprindum_coc                    1.874      0.534    0.5886     5.966
susprindum_pbc                    2.070      0.483    0.6777     6.321
susprindum_mar                    3.241      0.309    0.9861    10.649

Concordance= 0.692  (se = 0.025 )
Likelihood ratio test= 50.8  on 20 df,   p=2e-04
Wald test            = 237  on 20 df,   p=<2e-16
Score (logrank) test = 48.5  on 20 df,   p=4e-04,   Robust = 40.6  p=0.004

  (Note: the likelihood ratio and score tests assume independence of
     observations within a cluster, the Wald and robust score tests do not).
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.01    0.19    0.35    0.62    0.65    9.87 
Code
data_mine_miss_proc2_iiw_strata <- NULL # Initialize empty result

  for (i in 1:nrow(comb_strata)) {
    subset_data <- subset(dplyr::mutate(data_mine_miss_restr_proc2, rn=dplyr::row_number()), subset= tipo_de_plan_2_mod==comb_strata[i,"Var1"] & time_interval3== comb_strata[i,"Var2"])
    
    subset_data2<- cbind.data.frame(subset_data, iiw_strata= iiws_strat[[i]][[1]][[1]])
    
    data_mine_miss_proc2_iiw_strata <- rbind.data.frame(data_mine_miss_proc2_iiw_strata, subset_data2)
  }

#:#:#:#:#:#:#:#:#:#:#:#:#
#:#:#:#:#:#:#:#:#:#:#:#:#
#:#:#:#:#:#:#:#:#:#:#:#:#

data_mine_miss_proc2_iiw_strata_alt <- NULL # Initialize empty result

  for (i in 1:nrow(comb_strata_alt)) {
    subset_data_alt <- subset(dplyr::mutate(data_mine_miss_restr_proc2, rn=dplyr::row_number()), subset= tipo_de_plan_2_mod==comb_strata_alt[i,"Var1"] & time_interval3_alt== comb_strata_alt[i,"Var2"])
    
    subset_data2_alt<- cbind.data.frame(subset_data_alt, iiw_strata= iiws_strat_alt[[i]][[1]][[1]])
    
    data_mine_miss_proc2_iiw_strata_alt <- rbind.data.frame(data_mine_miss_proc2_iiw_strata_alt, subset_data2_alt)
  }


#:#:#:#:#:#:#:#:#:#:#:#:#
#:#:#:#:#:#:#:#:#:#:#:#:#
#:#:#:#:#:#:#:#:#:#:#:#:#

data_mine_miss_proc2_iiw_strata_alt_alt <- NULL # Initialize empty result

  for (i in 1:nrow(comb_strata_alt_alt)) {
    subset_data_alt <- subset(dplyr::mutate(data_mine_miss_restr_proc2, rn=dplyr::row_number()), subset= tipo_de_plan_2_mod==comb_strata_alt_alt[i,"Var1"] & time_interval3_alt== comb_strata_alt_alt[i,"Var2"])
    
    subset_data2_alt_alt<- cbind.data.frame(subset_data_alt, iiw_strata= iiws_strat_alt_alt[[i]][[1]][[1]])
    
    data_mine_miss_proc2_iiw_strata_alt_alt <- rbind.data.frame(data_mine_miss_proc2_iiw_strata_alt_alt, subset_data2_alt_alt)
  }

#:#:#:#:#:#:#:#:#:#:#:#:#
#:#:#:#:#:#:#:#:#:#:#:#:#
#:#:#:#:#:#:#:#:#:#:#:#:#

summary(data_mine_miss_proc2_iiw_strata$iiw_strata)
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
0.00e+00 1.00e+00 1.00e+00 9.60e+64 3.00e+00 2.98e+69 
Code
# Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
# 0.18    0.77    0.91    0.91    1.04   10.94 

summary(data_mine_miss_proc2_iiw_strata_alt$iiw_strata)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.01    0.60    0.86    1.07    1.18   26.75 
Code
 # Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 # 0.01    0.61    0.87    1.08    1.20   29.96 

summary(data_mine_miss_proc2_iiw_strata_alt_alt$iiw_strata)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
    0.0     0.3     0.4     0.7     0.8    42.1 
Code
  # Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  #   0.0     0.3     0.4     0.7     0.8    42.1 

invisible("Más confiable el 2do por LEJOS")
  
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
invisible("Stabilize weights")
data_mine_miss_proc2_iiw_strata$iiw_strata_st<-data_mine_miss_proc2_iiw_strata$iiw_strata

data_mine_miss_proc2_iiw_strata$iiw_strata_st[data_mine_miss_proc2_iiw_strata$iiw_strata>quantile(data_mine_miss_proc2_iiw_strata$iiw_strata,0.975)] <- quantile(data_mine_miss_proc2_iiw_strata$iiw_strata,0.975)
data_mine_miss_proc2_iiw_strata$iiw_strata_st[data_mine_miss_proc2_iiw_strata$iiw_after_ph<quantile(data_mine_miss_proc2_iiw_strata$iiw_strata,0.025)] <- quantile(data_mine_miss_proc2_iiw_strata$iiw_strata,0.025)

invisible("stabilize alternative weights")
data_mine_miss_proc2_iiw_strata_alt$iiw_strata_st<-data_mine_miss_proc2_iiw_strata_alt$iiw_strata

data_mine_miss_proc2_iiw_strata_alt$iiw_strata_st[data_mine_miss_proc2_iiw_strata_alt$iiw_strata>quantile(data_mine_miss_proc2_iiw_strata_alt$iiw_strata,0.975)] <- quantile(data_mine_miss_proc2_iiw_strata_alt$iiw_strata,0.975)
data_mine_miss_proc2_iiw_strata_alt$iiw_strata_st[data_mine_miss_proc2_iiw_strata_alt$iiw_strata<quantile(data_mine_miss_proc2_iiw_strata_alt$iiw_strata,0.025)] <- quantile(data_mine_miss_proc2_iiw_strata_alt$iiw_strata,0.025)

data_mine_miss_proc2_iiw_strata_alt_alt$iiw_strata_st<-data_mine_miss_proc2_iiw_strata_alt_alt$iiw_strata

data_mine_miss_proc2_iiw_strata_alt_alt$iiw_strata_st[data_mine_miss_proc2_iiw_strata_alt_alt$iiw_strata>quantile(data_mine_miss_proc2_iiw_strata_alt$iiw_strata,0.975)] <- quantile(data_mine_miss_proc2_iiw_strata_alt_alt$iiw_strata,0.975)
data_mine_miss_proc2_iiw_strata_alt_alt$iiw_strata_st[data_mine_miss_proc2_iiw_strata_alt_alt$iiw_strata<quantile(data_mine_miss_proc2_iiw_strata_alt$iiw_strata,0.025)] <- quantile(data_mine_miss_proc2_iiw_strata_alt_alt$iiw_strata,0.025)


summary(data_mine_miss_proc2_iiw_strata$iiw_strata_st)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.00    0.60    1.32    1.73    2.56    5.45 
Code
   # Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   # 0.00    0.78    1.37    1.88    2.67    5.68 

summary(data_mine_miss_proc2_iiw_strata_alt$iiw_strata_st)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.288   0.599   0.863   0.987   1.184   3.018 
Code
#  Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
# 0.292   0.614   0.873   1.003   1.196   3.121 

summary(data_mine_miss_proc2_iiw_strata_alt_alt$iiw_strata_st)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.116   0.116   0.439   0.605   0.761   3.069 
Code
  #  Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  # 0.116   0.116   0.439   0.603   0.761   3.118 

rbind.data.frame(
cbind.data.frame(model= "Primary (strata, lag=0)", t(matrix(summary(data_mine_miss_proc2_iiw_strata$iiw_strata_st)))),
cbind.data.frame(model= "Alternative (strata, lag=1)", t(matrix(summary(data_mine_miss_proc2_iiw_strata_alt$iiw_strata_st)))),
cbind.data.frame(model= "Alternative (2nd strata intervals, lag=1)", t(matrix(summary(data_mine_miss_proc2_iiw_strata_alt$iiw_strata_st)))))%>% 
  { 
    write.table(., file = paste0(getwd(),"/_proposal_grant/2023/iiw_strat_240503.csv"), dec=",", sep="\t")
    knitr::kable(., size=10, format="markdown", caption="Weights (stratifying)", col.names= c("Weight",attr(summary(data_mine_miss_proc2_iiw_strata$iiw_strata_st),"names")) ) 
  }
Weights (stratifying)
Weight Min. 1st Qu. Median Mean 3rd Qu. Max.
Primary (strata, lag=0) 0.000 0.599 1.324 1.734 2.56 5.45
Alternative (strata, lag=1) 0.288 0.599 0.863 0.987 1.18 3.02
Alternative (2nd strata intervals, lag=1) 0.288 0.599 0.863 0.987 1.18 3.02
GEE
Code
#The id= option is where we specify the clusters within which we have repeated observations that may be correlated, and tehe scale.fix=T option is to avoid R's default approach of introducing an overdispersion scale parameter.
#However, caution is advised when employing QIC and its use should not be routine, see Wang et al. (2015). growing use of quasi-likelihood-based information criteria for longitudinal data to select a working correlation structure in a generalized estimating equation framework. [https://onlinelibrary.wiley.com/doi/full/10.1002/sta4.95]

#poisson(link = "log"),

#https://www.thelancet.com/cms/10.1016/S2468-2667(22)00042-1/attachment/eb69ffc4-cb6c-4a47-9524-94e1ba9302b2/mmc1.pdf
#https://www.thelancet.com/cms/10.1016/S2468-2667(22)00201-8/attachment/05a17ae1-1e31-4ffd-a727-78ec915dc03b/mmc1.pdf

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

invisible("Must be independence structure")

m_strata <- geeglm( tr_outcome  ~ policonsumo2 +
            edad_al_ing_1 + 
            ano_nac_corr + 
            susinidum_oh +
            susinidum_coc +
            susinidum_pbc +
            susinidum_mar +
            psycom_dum_study +
            psycom_dum_with +
            freq_cons_dum_5day +
            cond_oc_dum_2inact +
            cond_oc_dum_3unemp +
            susprindum_coc +
            susprindum_pbc +
            susprindum_mar, 
id=id, data=data_mine_miss_proc2_iiw_strata, family=binomial, corstr="independence")
summary(m)

Call:
geeglm(formula = tr_outcome ~ policonsumo2 + edad_al_ing_1 + 
    ano_nac_corr + susinidum_oh + susinidum_coc + susinidum_pbc + 
    susinidum_mar + psycom_dum_study + psycom_dum_with + freq_cons_dum_5day + 
    cond_oc_dum_2inact + cond_oc_dum_3unemp + susprindum_coc + 
    susprindum_pbc + susprindum_mar, family = binomial, data = data_mine_miss_restr_proc2_iiw_after_ph, 
    id = id, corstr = "independence")

 Coefficients:
                    Estimate   Std.err   Wald Pr(>|W|)    
(Intercept)        -43.33863  13.16878  10.83  0.00100 ***
policonsumo2         0.05760   0.03597   2.56  0.10927    
edad_al_ing_1       -0.00669   0.00668   1.01  0.31608    
ano_nac_corr         0.02251   0.00654  11.83  0.00058 ***
susinidum_oh        -0.07388   0.09434   0.61  0.43356    
susinidum_coc       -0.01507   0.11807   0.02  0.89845    
susinidum_pbc        0.14542   0.10781   1.82  0.17737    
susinidum_mar        0.04749   0.09658   0.24  0.62290    
psycom_dum_study     0.86346   0.04632 347.54  < 2e-16 ***
psycom_dum_with      0.06046   0.03263   3.43  0.06392 .  
freq_cons_dum_5day  -0.02979   0.03089   0.93  0.33480    
cond_oc_dum_2inact  -0.25843   0.04290  36.29  1.7e-09 ***
cond_oc_dum_3unemp  -0.19282   0.03449  31.25  2.3e-08 ***
susprindum_coc       0.02959   0.04840   0.37  0.54105    
susprindum_pbc       0.23707   0.04089  33.61  6.7e-09 ***
susprindum_mar       0.06276   0.07496   0.70  0.40244    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Correlation structure = independence 
Estimated Scale Parameters:

            Estimate Std.err
(Intercept)        1  0.0162
Number of clusters:   13317  Maximum cluster size: 10 
Code
m_strata_full <- geeglm( tr_outcome  ~ policonsumo2 +
          comp_bpsc_y2_moderate+ 
          comp_bpsc_y3_severe+ 
          edad_al_ing_1+ 
          ano_nac_corr+ 
          esc_dum_rec_3prim+ 
          esc_dum_rec_2high +
          susprindum_oh +
          susprindum_coc +
          susprindum_pbc +
          susprindum_mar+
          freq_cons_dum_5day+
          freq_cons_dum_44to6wk+
          freq_cons_dum_32to3wk+
          freq_cons_dum_21wkmore+
          cond_oc_dum_3unemp+
          cond_oc_dum_2inact+
          viv_dum_illegal+
          viv_dum_own+
          viv_dum_rent+
          viv_dum_temp+
          psycom_dum_with+
          psycom_dum_study+
          susinidum_oh +
          susinidum_coc +
          susinidum_pbc +
          susinidum_mar+
          cohab_dum_alone+
          cohab_dum_fam_or+
          cohab_dum_cpl_child, 
id=id, data=data_mine_miss_proc2_iiw_strata, family=binomial, corstr="independence") 


rbind.data.frame(cbind.data.frame(model="simplest", broom::tidy(m_strata, exponentiate = T, conf.int=T)),cbind.data.frame(model="full", broom::tidy(m_strata_full, exponentiate = T, conf.int=T))) %>% dplyr::mutate_at(c("estimate","std.error","conf.low","conf.high"), ~sprintf("%1.2f",.))%>% dplyr::mutate_at(c("p.value"), ~sprintf("%1.4f",.)) %>% dplyr::select(-statistic) %>% 
  { 
    write.table(., file = paste0(getwd(),"/_proposal_grant/2023/gee_strata_240503.csv"), dec=",", sep="\t")
    knitr::kable(dplyr::filter(.,term=="policonsumo2"), size=10, format="markdown", caption="GEE Models (Stratifying)") 
  }
GEE Models (Stratifying)
model term estimate std.error p.value conf.low conf.high
simplest policonsumo2 1.06 0.04 0.1095 0.99 1.14
full policonsumo2 1.10 0.04 0.0080 1.03 1.18

Warning in eval(family$initialize): non-integer #successes in a binomial glm!


Call:
geeglm(formula = tr_outcome ~ policonsumo2 + edad_al_ing_1 + 
    ano_nac_corr + susinidum_oh + susinidum_coc + susinidum_pbc + 
    susinidum_mar + psycom_dum_study + psycom_dum_with + freq_cons_dum_5day + 
    cond_oc_dum_2inact + cond_oc_dum_3unemp + susprindum_coc + 
    susprindum_pbc + susprindum_mar, family = poisson(link = "log"), 
    data = data_mine_miss_proc2_iiw_strata, weights = iiw_strata_st, 
    id = id, corstr = "independence")

 Coefficients:
                    Estimate   Std.err  Wald Pr(>|W|)    
(Intercept)        -5.66e+00  3.86e+00  2.15  0.14247    
policonsumo2        2.83e-02  1.18e-02  5.74  0.01660 *  
edad_al_ing_1      -5.53e-03  1.96e-03  7.98  0.00473 ** 
ano_nac_corr        2.77e-03  1.92e-03  2.09  0.14849    
susinidum_oh        3.19e-03  3.21e-02  0.01  0.92074    
susinidum_coc       1.84e-02  3.77e-02  0.24  0.62594    
susinidum_pbc       4.83e-02  3.43e-02  1.98  0.15958    
susinidum_mar       2.61e-02  3.22e-02  0.66  0.41722    
psycom_dum_study    9.76e-02  1.09e-02 80.24  < 2e-16 ***
psycom_dum_with    -2.87e-05  1.03e-02  0.00  0.99776    
freq_cons_dum_5day  1.17e-03  9.13e-03  0.02  0.89794    
cond_oc_dum_2inact -5.28e-02  1.32e-02 15.94  6.5e-05 ***
cond_oc_dum_3unemp -3.80e-02  9.94e-03 14.64  0.00013 ***
susprindum_coc      1.99e-02  1.57e-02  1.60  0.20597    
susprindum_pbc      6.08e-02  1.31e-02 21.54  3.5e-06 ***
susprindum_mar      2.28e-02  2.21e-02  1.06  0.30356    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Correlation structure = independence 
Estimated Scale Parameters:

            Estimate Std.err
(Intercept)    0.253 0.00356
Number of clusters:   14073  Maximum cluster size: 8 

Warning in eval(family$initialize): non-integer #successes in a binomial glm!

GEE Models, weighted (Stratifying)
model term estimate std.error p.value conf.low conf.high
simplest, binomial policonsumo2 1.10 0.04 0.0363 1.01 1.20
full, binomial policonsumo2 1.13 0.04 0.0067 1.03 1.23
simplest, poisson policonsumo2 1.03 0.01 0.0166 1.01 1.05
full, poisson policonsumo2 1.04 0.01 0.0036 1.01 1.06
Code
# can use to fit a weighted GEE
mw_alt2 <- geeglm( tr_outcome  ~ policonsumo2 +
            edad_al_ing_1 + 
            ano_nac_corr + 
            susinidum_oh +
            susinidum_coc +
            susinidum_pbc +
            susinidum_mar +
            psycom_dum_study +
            psycom_dum_with +
            freq_cons_dum_5day +
            cond_oc_dum_2inact +
            cond_oc_dum_3unemp +
            susprindum_coc +
            susprindum_pbc +
            susprindum_mar, 
            id=id, data=data_mine_miss_proc2_iiw_strata_alt_alt, family= binomial, weights=iiw_strata_st, corstr="independence")

Warning in eval(family$initialize): non-integer #successes in a binomial glm!

Code
#In eval(family$initialize) : non-integer #successes in a binomial glm!
#summary(mw)

mwpois_alt2 <- geeglm( tr_outcome  ~ policonsumo2 +
            edad_al_ing_1 + 
            ano_nac_corr + 
            susinidum_oh +
            susinidum_coc +
            susinidum_pbc +
            susinidum_mar +
            psycom_dum_study +
            psycom_dum_with +
            freq_cons_dum_5day +
            cond_oc_dum_2inact +
            cond_oc_dum_3unemp +
            susprindum_coc +
            susprindum_pbc +
            susprindum_mar, 
            id=id, data=data_mine_miss_proc2_iiw_strata_alt_alt, family= poisson(link = "log"), weights=iiw_strata_st, corstr="independence")

mwpois_alt2 <- geeglm( tr_outcome  ~ policonsumo2 +
            edad_al_ing_1 + 
            ano_nac_corr + 
            susinidum_oh +
            susinidum_coc +
            susinidum_pbc +
            susinidum_mar +
            psycom_dum_study +
            psycom_dum_with +
            freq_cons_dum_5day +
            cond_oc_dum_2inact +
            cond_oc_dum_3unemp +
            susprindum_coc +
            susprindum_pbc +
            susprindum_mar, 
            id=id, data=data_mine_miss_proc2_iiw_strata_alt_alt, family= poisson(link = "log"), weights=iiw_strata_st, corstr="independence")


#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

mw_alt_full <- geeglm( tr_outcome  ~ policonsumo2 +
           comp_bpsc_y2_moderate+ 
          comp_bpsc_y3_severe+ 
          edad_al_ing_1+ 
          ano_nac_corr+ 
          esc_dum_rec_3prim+ 
          esc_dum_rec_2high +
          susprindum_oh +
          susprindum_coc +
          susprindum_pbc +
          susprindum_mar+
          freq_cons_dum_5day+
          freq_cons_dum_44to6wk+
          freq_cons_dum_32to3wk+
          freq_cons_dum_21wkmore+
          cond_oc_dum_3unemp+
          cond_oc_dum_2inact+
          viv_dum_illegal+
          viv_dum_own+
          viv_dum_rent+
          viv_dum_temp+
          psycom_dum_with+
          psycom_dum_study+
          susinidum_oh +
          susinidum_coc +
          susinidum_pbc +
          susinidum_mar+
          cohab_dum_alone+
          cohab_dum_fam_or+
          cohab_dum_cpl_child, 
id=id, data=data_mine_miss_proc2_iiw_strata_alt, family=binomial, weights=iiw_strata_st, corstr="independence") 

Warning in eval(family$initialize): non-integer #successes in a binomial glm!

Code
#In eval(family$initialize) : non-integer #successes in a binomial glm!

mw_alt2_full <- geeglm( tr_outcome  ~ policonsumo2 +
           comp_bpsc_y2_moderate+ 
          comp_bpsc_y3_severe+ 
          edad_al_ing_1+ 
          ano_nac_corr+ 
          esc_dum_rec_3prim+ 
          esc_dum_rec_2high +
          susprindum_oh +
          susprindum_coc +
          susprindum_pbc +
          susprindum_mar+
          freq_cons_dum_5day+
          freq_cons_dum_44to6wk+
          freq_cons_dum_32to3wk+
          freq_cons_dum_21wkmore+
          cond_oc_dum_3unemp+
          cond_oc_dum_2inact+
          viv_dum_illegal+
          viv_dum_own+
          viv_dum_rent+
          viv_dum_temp+
          psycom_dum_with+
          psycom_dum_study+
          susinidum_oh +
          susinidum_coc +
          susinidum_pbc +
          susinidum_mar+
          cohab_dum_alone+
          cohab_dum_fam_or+
          cohab_dum_cpl_child, 
id=id, data=data_mine_miss_proc2_iiw_strata_alt_alt, family=binomial, weights=iiw_strata_st, corstr="independence")

Warning in eval(family$initialize): non-integer #successes in a binomial glm!

Code
mwpois_alt_full <- geeglm( tr_outcome  ~ policonsumo2 +
           comp_bpsc_y2_moderate+ 
          comp_bpsc_y3_severe+ 
          edad_al_ing_1+ 
          ano_nac_corr+ 
          esc_dum_rec_3prim+ 
          esc_dum_rec_2high +
          susprindum_oh +
          susprindum_coc +
          susprindum_pbc +
          susprindum_mar+
          freq_cons_dum_5day+
          freq_cons_dum_44to6wk+
          freq_cons_dum_32to3wk+
          freq_cons_dum_21wkmore+
          cond_oc_dum_3unemp+
          cond_oc_dum_2inact+
          viv_dum_illegal+
          viv_dum_own+
          viv_dum_rent+
          viv_dum_temp+
          psycom_dum_with+
          psycom_dum_study+
          susinidum_oh +
          susinidum_coc +
          susinidum_pbc +
          susinidum_mar+
          cohab_dum_alone+
          cohab_dum_fam_or+
          cohab_dum_cpl_child, 
id=id, data=data_mine_miss_proc2_iiw_strata_alt, family= poisson(link = "log"), weights=iiw_strata_st, corstr="independence") 




mwpois_alt2_full <- geeglm( tr_outcome  ~ policonsumo2 +
           comp_bpsc_y2_moderate+ 
          comp_bpsc_y3_severe+ 
          edad_al_ing_1+ 
          ano_nac_corr+ 
          esc_dum_rec_3prim+ 
          esc_dum_rec_2high +
          susprindum_oh +
          susprindum_coc +
          susprindum_pbc +
          susprindum_mar+
          freq_cons_dum_5day+
          freq_cons_dum_44to6wk+
          freq_cons_dum_32to3wk+
          freq_cons_dum_21wkmore+
          cond_oc_dum_3unemp+
          cond_oc_dum_2inact+
          viv_dum_illegal+
          viv_dum_own+
          viv_dum_rent+
          viv_dum_temp+
          psycom_dum_with+
          psycom_dum_study+
          susinidum_oh +
          susinidum_coc +
          susinidum_pbc +
          susinidum_mar+
          cohab_dum_alone+
          cohab_dum_fam_or+
          cohab_dum_cpl_child, 
id=id, data=data_mine_miss_proc2_iiw_strata_alt_alt, family= poisson(link = "log"), weights=iiw_strata_st, corstr="independence") 

invisible("Model fit")
#broom::glance(mwpois_full)
#QIC(mw)


#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

rbind.data.frame(cbind.data.frame(model="simplest, binomial", broom::tidy(mw_alt, exponentiate = T, conf.int=T)),cbind.data.frame(model="full, binomial", broom::tidy(mw_alt_full, exponentiate = T, conf.int=T)),cbind.data.frame(model="simplest, poisson", broom::tidy(mwpois_alt, exponentiate = T, conf.int=T)),cbind.data.frame(model="full, poisson", broom::tidy(mwpois_alt_full, exponentiate = T, conf.int=T)),
                cbind.data.frame(model="simplest, binomial (alt)", broom::tidy(mw_alt2, exponentiate = T, conf.int=T)),cbind.data.frame(model="full, binomial (alt)", broom::tidy(mw_alt2_full, exponentiate = T, conf.int=T)),cbind.data.frame(model="simplest, poisson (alt)", broom::tidy(mwpois_alt2, exponentiate = T, conf.int=T)),cbind.data.frame(model="full, poisson (alt)", broom::tidy(mwpois_alt2_full, exponentiate = T, conf.int=T))
                 ) %>% dplyr::mutate_at(c("estimate","std.error","conf.low","conf.high"), ~sprintf("%1.2f",.))%>% dplyr::mutate_at(c("p.value"), ~sprintf("%1.4f",.)) %>% dplyr::select(-statistic) %>% 
  { 
    write.table(., file = paste0(getwd(),"/_proposal_grant/2023/geew_strata_alt_240503.csv"), dec=",", sep="\t")
    knitr::kable(dplyr::filter(.,term=="policonsumo2"), size=10, format="markdown", caption="GEE Models, alternative weighting (Stratifying)") 
  }
GEE Models, alternative weighting (Stratifying)
model term estimate std.error p.value conf.low conf.high
simplest, binomial policonsumo2 1.10 0.05 0.0457 1.00 1.21
full, binomial policonsumo2 1.06 0.04 0.1576 0.98 1.14
simplest, poisson policonsumo2 1.03 0.01 0.0184 1.01 1.06
full, poisson policonsumo2 1.02 0.01 0.1014 1.00 1.04
simplest, binomial (alt) policonsumo2 1.03 0.05 0.4846 0.94 1.14
full, binomial (alt) policonsumo2 1.07 0.05 0.1612 0.97 1.18
simplest, poisson (alt) policonsumo2 1.01 0.01 0.3140 0.99 1.04
full, poisson (alt) policonsumo2 1.02 0.01 0.1078 1.00 1.05

No corrections

Code
#load("E:/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/20240503_avance_iiw.RData")
iiw_nocorr<-
iiw.weights(Surv(time.lag,time,event)~ 
            cluster(id)+ 
            tr_outcome.lag +
            log_dias_treat_imp_sin_na.lag +
            less_90d_tr1.lag+
            comp_bpsc_y3_severe.lag + 
            policonsumo2.lag + 
            edad_al_ing_1 + 
            ano_nac_corr + 
            susinidum_coc +
            susinidum_oh +
            susinidum_pbc +
            susinidum_mar +
            psycom_dum_with +
            psycom_dum_study + 
            freq_cons_dum_5day +
            cond_oc_dum_2inact +
            cond_oc_dum_3unemp +
            susprindum_oh +
            susprindum_coc +
            susprindum_pbc +
            susprindum_mar, 
            data= data_mine_miss_restr_proc2 %>% as.data.frame(),
            id= "id",
            time= "time",
            event= "event", #character string indicating which column of the data indicates whether or not a visit occurred. If every row corresponds to a visit, then this column will consist entirely of ones
            maxfu= maxfu_restr_df %>% as.data.frame(),
            # formulanull = Surv(time.lag,time,event)~ edad_al_ing_1 + 
            #       ano_nac_corr,
            invariant= c("edad_al_ing_1", "ano_nac_corr", "susinidum_coc", "susinidum_oh", "susinidum_pbc", "susinidum_mar",  "psycom_dum_with", "psycom_dum_study", "freq_cons_dum_5day", "cond_oc_dum_2inact", "cond_oc_dum_3unemp", "susprindum_oh", "susprindum_coc", "susprindum_pbc", "susprindum_mar"),
            lagvars= c("time", "log_dias_treat_imp_sin_na","tr_outcome", "comp_bpsc_y3_severe", "less_90d_tr1", "policonsumo2"),
            lagfirst= c(2.95082,4.499811/2,0,0,0,0),  #90/30.5 4.499811 es 90 días
            first= T
)

Warning in Surv(time.lag, time, event): Stop time must be > start time, NA created

Code
iiw_nocorr_alt<-
iiw.weights(Surv(time.lag,time,event)~ 
            cluster(id)+ 
            tr_outcome.lag +
            log_dias_treat_imp_sin_na.lag +
            less_90d_tr1.lag+
            comp_bpsc_y3_severe.lag + 
            policonsumo2.lag + 
            edad_al_ing_1 + 
            ano_nac_corr + 
            susinidum_coc +
            susinidum_oh +
            susinidum_pbc +
            susinidum_mar +
            psycom_dum_with +
            psycom_dum_study + 
            freq_cons_dum_5day +
            cond_oc_dum_2inact +
            cond_oc_dum_3unemp +
            susprindum_oh +
            susprindum_coc +
            susprindum_pbc +
            susprindum_mar, 
            data= data_mine_miss_restr_proc2 %>% as.data.frame(),
            id= "id",
            time= "time",
            event= "event", #character string indicating which column of the data indicates whether or not a visit occurred. If every row corresponds to a visit, then this column will consist entirely of ones
            maxfu= maxfu_restr_df %>% as.data.frame(),
            # formulanull = Surv(time.lag,time,event)~ edad_al_ing_1 + 
            #       ano_nac_corr,
            invariant= c("edad_al_ing_1", "ano_nac_corr", "susinidum_coc", "susinidum_oh", "susinidum_pbc", "susinidum_mar",  "psycom_dum_with", "psycom_dum_study", "freq_cons_dum_5day", "cond_oc_dum_2inact", "cond_oc_dum_3unemp", "susprindum_oh", "susprindum_coc", "susprindum_pbc", "susprindum_mar"),
            lagvars= c("time", "log_dias_treat_imp_sin_na","tr_outcome", "comp_bpsc_y3_severe", "less_90d_tr1", "policonsumo2"),
            lagfirst= c(2.95082,4.499811,1,1,1,1),  #90/30.5 2.95082 ; 4.499811 es 90 días
            first= T
)

Warning in Surv(time.lag, time, event): Stop time must be > start time, NA created

Code
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
Code
summary(iiw_nocorr$iiw)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.21    1.00    1.62    1.84    2.56    5.99 
Code
summary(iiw_nocorr_alt$iiw)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
    0.1     0.4     0.7     0.8     1.0   102.3 
Code
data_mine_miss_restr_proc2$iiw_nocorr<-iiw_nocorr$iiw
data_mine_miss_restr_proc2$iiw_nocorr_alt<-iiw_nocorr_alt$iiw

data_mine_miss_restr_proc2$iiw_nocorr_st<-data_mine_miss_restr_proc2$iiw_nocorr
data_mine_miss_restr_proc2$iiw_nocorr_st[data_mine_miss_restr_proc2$iiw_nocorr>quantile(data_mine_miss_restr_proc2$iiw_nocorr,0.975)] <- quantile(data_mine_miss_restr_proc2$iiw_nocorr,0.975)
data_mine_miss_restr_proc2$iiw_nocorr_st[data_mine_miss_restr_proc2_iiw_after_ph$iiw_nocorr<quantile(data_mine_miss_restr_proc2$iiw_nocorr,0.025)] <- quantile(data_mine_miss_restr_proc2$iiw_nocorr,0.025)


data_mine_miss_restr_proc2$iiw_nocorr_alt_st<-data_mine_miss_restr_proc2$iiw_nocorr_alt
data_mine_miss_restr_proc2$iiw_nocorr_alt_st[data_mine_miss_restr_proc2$iiw_nocorr_alt>quantile(data_mine_miss_restr_proc2$iiw_nocorr_alt,0.975)] <- quantile(data_mine_miss_restr_proc2$iiw_nocorr_alt,0.975)
data_mine_miss_restr_proc2$iiw_nocorr_st[data_mine_miss_restr_proc2_iiw_after_ph$iiw_iiw_nocorr_altnocorr<quantile(data_mine_miss_restr_proc2$iiw_nocorr_alt,0.025)] <- quantile(data_mine_miss_restr_proc2$iiw_nocorr_alt,0.025)

summary(iiw_nocorr$iiw)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.21    1.00    1.62    1.84    2.56    5.99 
Code
   # Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   # 0.53    1.00    1.54    1.87    2.67    6.03 
summary(iiw_nocorr_alt$iiw)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
    0.1     0.4     0.7     0.8     1.0   102.3 
Code
   # Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   #  0.1     0.4     0.7     0.8     1.0   102.3 
summary(data_mine_miss_restr_proc2$iiw_nocorr)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.21    1.00    1.62    1.84    2.56    5.99 
Code
   # Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   # 0.53    1.00    1.54    1.87    2.67    6.03 
summary(data_mine_miss_restr_proc2$iiw_nocorr_st)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.21    1.00    1.62    1.83    2.56    3.79 
Code
   # Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   # 0.53    1.00    1.54    1.86    2.67    3.89 
summary(data_mine_miss_restr_proc2$iiw_nocorr_alt_st)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.129   0.418   0.676   0.697   1.000   1.000 
Code
  #  Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  # 0.129   0.418   0.676   0.697   1.000   1.000 

rbind.data.frame(
cbind.data.frame(model= "Primary (no corrections, lag=0)", t(matrix(summary(data_mine_miss_restr_proc2$iiw_nocorr_st)))),
cbind.data.frame(model= "Alternative (no corrections, lag=1)", t(matrix(summary(data_mine_miss_restr_proc2$iiw_nocorr_alt_st)))))%>% 
  { 
    write.table(., file = paste0(getwd(),"/_proposal_grant/2023/iiw_nocorr_240503.csv"), dec=",", sep="\t")
    knitr::kable(., size=10, format="markdown", caption="Weights (No corrections)", col.names= c("Weight",attr(summary(data_mine_miss_restr_proc2$iiw_nocorr_st),"names")) ) 
  }
Weights (No corrections)
Weight Min. 1st Qu. Median Mean 3rd Qu. Max.
Primary (no corrections, lag=0) 0.214 1.000 1.617 1.834 2.56 3.79
Alternative (no corrections, lag=1) 0.129 0.418 0.676 0.697 1.00 1.00
GEE
Code
#The id= option is where we specify the clusters within which we have repeated observations that may be correlated, and tehe scale.fix=T option is to avoid R's default approach of introducing an overdispersion scale parameter.
#However, caution is advised when employing QIC and its use should not be routine, see Wang et al. (2015). growing use of quasi-likelihood-based information criteria for longitudinal data to select a working correlation structure in a generalized estimating equation framework. [https://onlinelibrary.wiley.com/doi/full/10.1002/sta4.95]

#poisson(link = "log"),

#https://www.thelancet.com/cms/10.1016/S2468-2667(22)00042-1/attachment/eb69ffc4-cb6c-4a47-9524-94e1ba9302b2/mmc1.pdf
#https://www.thelancet.com/cms/10.1016/S2468-2667(22)00201-8/attachment/05a17ae1-1e31-4ffd-a727-78ec915dc03b/mmc1.pdf

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

invisible("Must be independence structure")

m_nocorr <- geeglm( tr_outcome  ~ policonsumo2 +
            edad_al_ing_1 + 
            ano_nac_corr + 
            susinidum_oh +
            susinidum_coc +
            susinidum_pbc +
            susinidum_mar +
            psycom_dum_study +
            psycom_dum_with +
            freq_cons_dum_5day +
            cond_oc_dum_2inact +
            cond_oc_dum_3unemp +
            susprindum_coc +
            susprindum_pbc +
            susprindum_mar, 
id=id, data=data_mine_miss_restr_proc2, family=binomial, corstr="independence")
summary(m_nocorr)

Call:
geeglm(formula = tr_outcome ~ policonsumo2 + edad_al_ing_1 + 
    ano_nac_corr + susinidum_oh + susinidum_coc + susinidum_pbc + 
    susinidum_mar + psycom_dum_study + psycom_dum_with + freq_cons_dum_5day + 
    cond_oc_dum_2inact + cond_oc_dum_3unemp + susprindum_coc + 
    susprindum_pbc + susprindum_mar, family = binomial, data = data_mine_miss_restr_proc2, 
    id = id, corstr = "independence")

 Coefficients:
                    Estimate   Std.err   Wald Pr(>|W|)    
(Intercept)        -43.33863  13.16878  10.83  0.00100 ***
policonsumo2         0.05760   0.03597   2.56  0.10927    
edad_al_ing_1       -0.00669   0.00668   1.01  0.31608    
ano_nac_corr         0.02251   0.00654  11.83  0.00058 ***
susinidum_oh        -0.07388   0.09434   0.61  0.43356    
susinidum_coc       -0.01507   0.11807   0.02  0.89845    
susinidum_pbc        0.14542   0.10781   1.82  0.17737    
susinidum_mar        0.04749   0.09658   0.24  0.62290    
psycom_dum_study     0.86346   0.04632 347.54  < 2e-16 ***
psycom_dum_with      0.06046   0.03263   3.43  0.06392 .  
freq_cons_dum_5day  -0.02979   0.03089   0.93  0.33480    
cond_oc_dum_2inact  -0.25843   0.04290  36.29  1.7e-09 ***
cond_oc_dum_3unemp  -0.19282   0.03449  31.25  2.3e-08 ***
susprindum_coc       0.02959   0.04840   0.37  0.54105    
susprindum_pbc       0.23707   0.04089  33.61  6.7e-09 ***
susprindum_mar       0.06276   0.07496   0.70  0.40244    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Correlation structure = independence 
Estimated Scale Parameters:

            Estimate Std.err
(Intercept)        1  0.0162
Number of clusters:   13317  Maximum cluster size: 10 
Code
m_nocorr_full <- geeglm( tr_outcome  ~ policonsumo2 +
          comp_bpsc_y2_moderate+ 
          comp_bpsc_y3_severe+ 
          edad_al_ing_1+ 
          ano_nac_corr+ 
          esc_dum_rec_3prim+ 
          esc_dum_rec_2high +
          susprindum_oh +
          susprindum_coc +
          susprindum_pbc +
          susprindum_mar+
          freq_cons_dum_5day+
          freq_cons_dum_44to6wk+
          freq_cons_dum_32to3wk+
          freq_cons_dum_21wkmore+
          cond_oc_dum_3unemp+
          cond_oc_dum_2inact+
          viv_dum_illegal+
          viv_dum_own+
          viv_dum_rent+
          viv_dum_temp+
          psycom_dum_with+
          psycom_dum_study+
          susinidum_oh +
          susinidum_coc +
          susinidum_pbc +
          susinidum_mar+
          cohab_dum_alone+
          cohab_dum_fam_or+
          cohab_dum_cpl_child, 
id=id, data=data_mine_miss_restr_proc2, family=binomial, corstr="independence") 


rbind.data.frame(cbind.data.frame(model="simplest", broom::tidy(m_nocorr, exponentiate = T, conf.int=T)),cbind.data.frame(model="full", broom::tidy(m_nocorr_full, exponentiate = T, conf.int=T))) %>% dplyr::mutate_at(c("estimate","std.error","conf.low","conf.high"), ~sprintf("%1.2f",.))%>% dplyr::mutate_at(c("p.value"), ~sprintf("%1.4f",.)) %>% dplyr::select(-statistic) %>% 
  { 
    write.table(., file = paste0(getwd(),"/_proposal_grant/2023/gee_nocorr_240503.csv"), dec=",", sep="\t")
    knitr::kable(dplyr::filter(.,term=="policonsumo2"), size=10, format="markdown", caption="GEE Models (no correction)") 
  }
GEE Models (no correction)
model term estimate std.error p.value conf.low conf.high
simplest policonsumo2 1.06 0.04 0.1093 0.99 1.14
full policonsumo2 1.10 0.04 0.0079 1.03 1.18
Code
data_mine_miss_restr_proc2$edad_al_ing_1_st = data_mine_miss_restr_proc2$edad_al_ing_1 - mean(data_mine_miss_restr_proc2$edad_al_ing_1)

# can use to fit a weighted GEE
mw_nocorr <- geeglm( tr_outcome  ~ policonsumo2 +
            edad_al_ing_1 + 
            ano_nac_corr + 
            susinidum_oh +
            susinidum_coc +
            susinidum_pbc +
            susinidum_mar +
            psycom_dum_study +
            psycom_dum_with +
            freq_cons_dum_5day +
            cond_oc_dum_2inact +
            cond_oc_dum_3unemp +
            susprindum_coc +
            susprindum_pbc +
            susprindum_mar, 
            id=id, data=data_mine_miss_restr_proc2, family= binomial, weights=iiw_nocorr_st, corstr="independence")

Warning in eval(family$initialize): non-integer #successes in a binomial glm!

Code
#In eval(family$initialize) : non-integer #successes in a binomial glm!
#summary(mw)

mw_nocorr_pois <- geeglm( tr_outcome  ~ policonsumo2 +
            edad_al_ing_1 + 
            ano_nac_corr + 
            susinidum_oh +
            susinidum_coc +
            susinidum_pbc +
            susinidum_mar +
            psycom_dum_study +
            psycom_dum_with +
            freq_cons_dum_5day +
            cond_oc_dum_2inact +
            cond_oc_dum_3unemp +
            susprindum_coc +
            susprindum_pbc +
            susprindum_mar, 
            id=id, data=data_mine_miss_restr_proc2, family= poisson(link = "log"), weights=iiw_nocorr_st, corstr="independence")
summary(mwpois)

Call:
geeglm(formula = tr_outcome ~ policonsumo2 + edad_al_ing_1 + 
    ano_nac_corr + susinidum_oh + susinidum_coc + susinidum_pbc + 
    susinidum_mar + psycom_dum_study + psycom_dum_with + freq_cons_dum_5day + 
    cond_oc_dum_2inact + cond_oc_dum_3unemp + susprindum_coc + 
    susprindum_pbc + susprindum_mar, family = poisson(link = "log"), 
    data = data_mine_miss_restr_proc2_iiw_after_ph, weights = iiw_after_ph_st, 
    id = id, corstr = "independence")

 Coefficients:
                   Estimate  Std.err   Wald Pr(>|W|)    
(Intercept)        -6.48845  3.56281   3.32   0.0686 .  
policonsumo2        0.02903  0.01098   6.99   0.0082 ** 
edad_al_ing_1      -0.00520  0.00182   8.13   0.0044 ** 
ano_nac_corr        0.00318  0.00177   3.23   0.0724 .  
susinidum_oh       -0.01208  0.02868   0.18   0.6736    
susinidum_coc       0.00578  0.03446   0.03   0.8668    
susinidum_pbc       0.03984  0.03091   1.66   0.1975    
susinidum_mar       0.01794  0.02878   0.39   0.5331    
psycom_dum_study    0.13012  0.01024 161.40  < 2e-16 ***
psycom_dum_with     0.00609  0.01002   0.37   0.5436    
freq_cons_dum_5day  0.00429  0.00860   0.25   0.6179    
cond_oc_dum_2inact -0.05450  0.01246  19.14  1.2e-05 ***
cond_oc_dum_3unemp -0.03855  0.00943  16.71  4.4e-05 ***
susprindum_coc      0.01730  0.01487   1.35   0.2447    
susprindum_pbc      0.05929  0.01247  22.60  2.0e-06 ***
susprindum_mar      0.01361  0.02142   0.40   0.5252    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Correlation structure = independence 
Estimated Scale Parameters:

            Estimate Std.err
(Intercept)    0.256 0.00339
Number of clusters:   13317  Maximum cluster size: 10 
Code
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

mw_nocorr_full <- geeglm( tr_outcome  ~ policonsumo2 +
           comp_bpsc_y2_moderate+ 
          comp_bpsc_y3_severe+ 
          edad_al_ing_1+ 
          ano_nac_corr+ 
          esc_dum_rec_3prim+ 
          esc_dum_rec_2high +
          susprindum_oh +
          susprindum_coc +
          susprindum_pbc +
          susprindum_mar+
          freq_cons_dum_5day+
          freq_cons_dum_44to6wk+
          freq_cons_dum_32to3wk+
          freq_cons_dum_21wkmore+
          cond_oc_dum_3unemp+
          cond_oc_dum_2inact+
          viv_dum_illegal+
          viv_dum_own+
          viv_dum_rent+
          viv_dum_temp+
          psycom_dum_with+
          psycom_dum_study+
          susinidum_oh +
          susinidum_coc +
          susinidum_pbc +
          susinidum_mar+
          cohab_dum_alone+
          cohab_dum_fam_or+
          cohab_dum_cpl_child, 
id=id, data=data_mine_miss_restr_proc2, family=binomial, weights=iiw_nocorr_st, corstr="independence") 

Warning in eval(family$initialize): non-integer #successes in a binomial glm!

Code
#In eval(family$initialize) : non-integer #successes in a binomial glm!


mw_nocorr_pois_full <- geeglm( tr_outcome  ~ policonsumo2 +
           comp_bpsc_y2_moderate+ 
          comp_bpsc_y3_severe+ 
          edad_al_ing_1+ 
          ano_nac_corr+ 
          esc_dum_rec_3prim+ 
          esc_dum_rec_2high +
          susprindum_oh +
          susprindum_coc +
          susprindum_pbc +
          susprindum_mar+
          freq_cons_dum_5day+
          freq_cons_dum_44to6wk+
          freq_cons_dum_32to3wk+
          freq_cons_dum_21wkmore+
          cond_oc_dum_3unemp+
          cond_oc_dum_2inact+
          viv_dum_illegal+
          viv_dum_own+
          viv_dum_rent+
          viv_dum_temp+
          psycom_dum_with+
          psycom_dum_study+
          susinidum_oh +
          susinidum_coc +
          susinidum_pbc +
          susinidum_mar+
          cohab_dum_alone+
          cohab_dum_fam_or+
          cohab_dum_cpl_child, 
id=id, data=data_mine_miss_restr_proc2, family= poisson(link = "log"), weights=iiw_nocorr_st, corstr="independence") 

invisible("Model fit")
#broom::glance(mwpois_full)
#QIC(mw)


#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

rbind.data.frame(cbind.data.frame(model="simplest, binomial", broom::tidy(mw_nocorr, exponentiate = T, conf.int=T)),cbind.data.frame(model="full, binomial", broom::tidy(mw_nocorr_full, exponentiate = T, conf.int=T)),cbind.data.frame(model="simplest, poisson", broom::tidy(mw_nocorr_pois, exponentiate = T, conf.int=T)),cbind.data.frame(model="full, poisson", broom::tidy(mw_nocorr_pois_full, exponentiate = T, conf.int=T))) %>% dplyr::mutate_at(c("estimate","std.error","conf.low","conf.high"), ~sprintf("%1.2f",.))%>% dplyr::mutate_at(c("p.value"), ~sprintf("%1.4f",.)) %>% dplyr::select(-statistic) %>% 
  { 
    write.table(., file = paste0(getwd(),"/_proposal_grant/2023/geew_nocorr_240503.csv"), dec=",", sep="\t")
    knitr::kable(dplyr::filter(.,term=="policonsumo2"), size=10, format="markdown", caption="GEE Models, weighted (no correction)") 
  }
GEE Models, weighted (no correction)
model term estimate std.error p.value conf.low conf.high
simplest, binomial policonsumo2 1.08 0.04 0.0548 1.00 1.16
full, binomial policonsumo2 1.11 0.04 0.0063 1.03 1.20
simplest, poisson policonsumo2 1.02 0.01 0.0215 1.00 1.04
full, poisson policonsumo2 1.03 0.01 0.0029 1.01 1.05
Code
# can use to fit a weighted GEE
mw_nocorr_alt <- geeglm( tr_outcome  ~ policonsumo2 +
            edad_al_ing_1 + 
            ano_nac_corr + 
            susinidum_oh +
            susinidum_coc +
            susinidum_pbc +
            susinidum_mar +
            psycom_dum_study +
            psycom_dum_with +
            freq_cons_dum_5day +
            cond_oc_dum_2inact +
            cond_oc_dum_3unemp +
            susprindum_coc +
            susprindum_pbc +
            susprindum_mar, 
            id=id, data=data_mine_miss_restr_proc2, family= binomial, weights=iiw_nocorr_alt_st, corstr="independence")

Warning in eval(family$initialize): non-integer #successes in a binomial glm!

Code
#In eval(family$initialize) : non-integer #successes in a binomial glm!
#summary(mw)

mw_nocorr_pois_alt <- geeglm( tr_outcome  ~ policonsumo2 +
            edad_al_ing_1 + 
            ano_nac_corr + 
            susinidum_oh +
            susinidum_coc +
            susinidum_pbc +
            susinidum_mar +
            psycom_dum_study +
            psycom_dum_with +
            freq_cons_dum_5day +
            cond_oc_dum_2inact +
            cond_oc_dum_3unemp +
            susprindum_coc +
            susprindum_pbc +
            susprindum_mar, 
            id=id, data=data_mine_miss_restr_proc2, family= poisson(link = "log"), weights=iiw_nocorr_alt_st, corstr="independence")
summary(mwpois)

Call:
geeglm(formula = tr_outcome ~ policonsumo2 + edad_al_ing_1 + 
    ano_nac_corr + susinidum_oh + susinidum_coc + susinidum_pbc + 
    susinidum_mar + psycom_dum_study + psycom_dum_with + freq_cons_dum_5day + 
    cond_oc_dum_2inact + cond_oc_dum_3unemp + susprindum_coc + 
    susprindum_pbc + susprindum_mar, family = poisson(link = "log"), 
    data = data_mine_miss_restr_proc2_iiw_after_ph, weights = iiw_after_ph_st, 
    id = id, corstr = "independence")

 Coefficients:
                   Estimate  Std.err   Wald Pr(>|W|)    
(Intercept)        -6.48845  3.56281   3.32   0.0686 .  
policonsumo2        0.02903  0.01098   6.99   0.0082 ** 
edad_al_ing_1      -0.00520  0.00182   8.13   0.0044 ** 
ano_nac_corr        0.00318  0.00177   3.23   0.0724 .  
susinidum_oh       -0.01208  0.02868   0.18   0.6736    
susinidum_coc       0.00578  0.03446   0.03   0.8668    
susinidum_pbc       0.03984  0.03091   1.66   0.1975    
susinidum_mar       0.01794  0.02878   0.39   0.5331    
psycom_dum_study    0.13012  0.01024 161.40  < 2e-16 ***
psycom_dum_with     0.00609  0.01002   0.37   0.5436    
freq_cons_dum_5day  0.00429  0.00860   0.25   0.6179    
cond_oc_dum_2inact -0.05450  0.01246  19.14  1.2e-05 ***
cond_oc_dum_3unemp -0.03855  0.00943  16.71  4.4e-05 ***
susprindum_coc      0.01730  0.01487   1.35   0.2447    
susprindum_pbc      0.05929  0.01247  22.60  2.0e-06 ***
susprindum_mar      0.01361  0.02142   0.40   0.5252    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Correlation structure = independence 
Estimated Scale Parameters:

            Estimate Std.err
(Intercept)    0.256 0.00339
Number of clusters:   13317  Maximum cluster size: 10 
Code
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

mw_nocorr_alt_full <- geeglm( tr_outcome  ~ policonsumo2 +
           comp_bpsc_y2_moderate+ 
          comp_bpsc_y3_severe+ 
          edad_al_ing_1+ 
          ano_nac_corr+ 
          esc_dum_rec_3prim+ 
          esc_dum_rec_2high +
          susprindum_oh +
          susprindum_coc +
          susprindum_pbc +
          susprindum_mar+
          freq_cons_dum_5day+
          freq_cons_dum_44to6wk+
          freq_cons_dum_32to3wk+
          freq_cons_dum_21wkmore+
          cond_oc_dum_3unemp+
          cond_oc_dum_2inact+
          viv_dum_illegal+
          viv_dum_own+
          viv_dum_rent+
          viv_dum_temp+
          psycom_dum_with+
          psycom_dum_study+
          susinidum_oh +
          susinidum_coc +
          susinidum_pbc +
          susinidum_mar+
          cohab_dum_alone+
          cohab_dum_fam_or+
          cohab_dum_cpl_child, 
id=id, data=data_mine_miss_restr_proc2, family=binomial, weights=iiw_nocorr_alt_st, corstr="independence") 

Warning in eval(family$initialize): non-integer #successes in a binomial glm!

Code
#In eval(family$initialize) : non-integer #successes in a binomial glm!


mw_nocorr_pois_alt_full <- geeglm( tr_outcome  ~ policonsumo2 +
           comp_bpsc_y2_moderate+ 
          comp_bpsc_y3_severe+ 
          edad_al_ing_1+ 
          ano_nac_corr+ 
          esc_dum_rec_3prim+ 
          esc_dum_rec_2high +
          susprindum_oh +
          susprindum_coc +
          susprindum_pbc +
          susprindum_mar+
          freq_cons_dum_5day+
          freq_cons_dum_44to6wk+
          freq_cons_dum_32to3wk+
          freq_cons_dum_21wkmore+
          cond_oc_dum_3unemp+
          cond_oc_dum_2inact+
          viv_dum_illegal+
          viv_dum_own+
          viv_dum_rent+
          viv_dum_temp+
          psycom_dum_with+
          psycom_dum_study+
          susinidum_oh +
          susinidum_coc +
          susinidum_pbc +
          susinidum_mar+
          cohab_dum_alone+
          cohab_dum_fam_or+
          cohab_dum_cpl_child, 
id=id, data=data_mine_miss_restr_proc2, family= poisson(link = "log"), weights=iiw_nocorr_alt_st, corstr="independence") 

invisible("Model fit")
#broom::glance(mwpois_full)
#QIC(mw)


#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

rbind.data.frame(cbind.data.frame(model="simplest, binomial", broom::tidy(mw_nocorr_alt, exponentiate = T, conf.int=T)), cbind.data.frame(model="full, binomial", broom::tidy(mw_nocorr_alt_full, exponentiate = T, conf.int=T)), cbind.data.frame(model="simplest, poisson", broom::tidy(mw_nocorr_pois_alt, exponentiate = T, conf.int=T)),cbind.data.frame(model="full, poisson", broom::tidy(mw_nocorr_pois_alt_full, exponentiate = T, conf.int=T))) %>% dplyr::mutate_at(c("estimate","std.error","conf.low","conf.high"), ~sprintf("%1.2f",.))%>% dplyr::mutate_at(c("p.value"), ~sprintf("%1.4f",.)) %>% dplyr::select(-statistic) %>% 
  { 
    write.table(., file = paste0(getwd(),"/_proposal_grant/2023/geew_nocorr_alt_240503.csv"), dec=",", sep="\t")
    knitr::kable(dplyr::filter(.,term=="policonsumo2"), size=10, format="markdown", caption="GEE Models, alternative weighting (no correction)") 
  }
GEE Models, alternative weighting (no correction)
model term estimate std.error p.value conf.low conf.high
simplest, binomial policonsumo2 1.04 0.04 0.2923 0.97 1.12
full, binomial policonsumo2 1.09 0.04 0.0321 1.01 1.17
simplest, poisson policonsumo2 1.01 0.01 0.1631 0.99 1.03
full, poisson policonsumo2 1.02 0.01 0.0206 1.00 1.04


Session info

Code
message(paste0("R library: ", Sys.getenv("R_LIBS_USER")))

R library: E:/Mi unidad/Alvacast/SISTRAT 2022 (github)/renv/library/R-4.1/x86_64-w64-mingw32

Code
message(paste0("Date: ",withr::with_locale(new = c('LC_TIME' = 'C'), code =Sys.time())))

Date: 2024-05-27 22:36:56

Code
message(paste0("Editor context: ", path))

Editor context: C:/Users/CISS Fondecyt/Mi unidad/Alvacast/SISTRAT 2022 (github)/env

Code
sesion_info <- devtools::session_info()
dplyr::select(
  tibble::as_tibble(sesion_info$packages),
  c(package, loadedversion, source)
) %>% 
 kable(caption = "R packages", format = "markdown",
      col.names = c("Row number", "Package", "Version"),
    row.names = FALSE,
      align = c("c", "l", "r"))
R packages
Row number Package Version
abind 1.4-5 CRAN (R 4.1.1)
arrow 15.0.1 CRAN (R 4.1.2)
assertthat 0.2.1 CRAN (R 4.1.2)
backports 1.4.1 CRAN (R 4.1.2)
base64enc 0.1-3 CRAN (R 4.1.1)
BiocManager 1.30.18 CRAN (R 4.1.3)
bit 4.0.4 CRAN (R 4.1.2)
bit64 4.0.5 CRAN (R 4.1.2)
blob 1.2.3 CRAN (R 4.1.3)
boot 1.3-28.1 CRAN (R 4.1.3)
broom 1.0.1 CRAN (R 4.1.3)
cachem 1.0.6 CRAN (R 4.1.2)
callr 3.7.2 CRAN (R 4.1.3)
car 3.1-0 CRAN (R 4.1.3)
carData 3.0-5 CRAN (R 4.1.2)
cellranger 1.1.0 CRAN (R 4.1.2)
checkmate 2.1.0 CRAN (R 4.1.3)
chron 2.3-58 CRAN (R 4.1.3)
class 7.3-20 CRAN (R 4.1.3)
cli 3.4.1 CRAN (R 4.1.3)
clisymbols 1.2.0 CRAN (R 4.1.3)
cluster 2.1.4 CRAN (R 4.1.3)
codetools 0.2-19 CRAN (R 4.1.3)
colorspace 2.0-3 CRAN (R 4.1.2)
compareGroups 4.5.1 CRAN (R 4.1.3)
cowplot 1.1.1 CRAN (R 4.1.2)
crayon 1.5.2 CRAN (R 4.1.3)
curl 4.3.3 CRAN (R 4.1.3)
DALEX 2.4.3 CRAN (R 4.1.3)
data.table 1.14.2 CRAN (R 4.1.2)
data.tree 1.0.0 CRAN (R 4.1.3)
DBI 1.1.3 CRAN (R 4.1.3)
dbplyr 2.2.1 CRAN (R 4.1.3)
deldir 1.0-6 CRAN (R 4.1.1)
devtools 2.4.4 CRAN (R 4.1.3)
DiagrammeR 1.0.9 CRAN (R 4.1.3)
DiagrammeRsvg 0.1 CRAN (R 4.1.3)
digest 0.6.29 CRAN (R 4.1.3)
doRNG 1.8.2 CRAN (R 4.1.3)
dplyr 1.0.10 CRAN (R 4.1.3)
DT 0.26 CRAN (R 4.1.3)
e1071 1.7-11 CRAN (R 4.1.3)
easyalluvial 0.3.2 CRAN (R 4.1.2)
ellipsis 0.3.2 CRAN (R 4.1.2)
evaluate 0.16 CRAN (R 4.1.3)
fansi 1.0.3 CRAN (R 4.1.3)
farver 2.1.1 CRAN (R 4.1.3)
fastDummies 1.7.3 CRAN (R 4.1.2)
fastmap 1.1.0 CRAN (R 4.1.3)
flextable 0.8.2 CRAN (R 4.1.3)
FNN 1.1.3.1 CRAN (R 4.1.3)
forcats 0.5.2 CRAN (R 4.1.3)
foreach 1.5.2 CRAN (R 4.1.2)
foreign 0.8-83 CRAN (R 4.1.3)
Formula 1.2-4 CRAN (R 4.1.1)
fs 1.5.2 CRAN (R 4.1.3)
future 1.28.0 CRAN (R 4.1.3)
future.apply 1.9.1 CRAN (R 4.1.3)
gargle 1.2.1 CRAN (R 4.1.3)
gdtools 0.2.4 CRAN (R 4.1.2)
gee 4.13-26 CRAN (R 4.1.2)
geepack 1.3.9 CRAN (R 4.1.3)
geesmv 1.3 CRAN (R 4.1.3)
generics 0.1.3 CRAN (R 4.1.3)
ggalluvial 0.12.5 CRAN (R 4.1.3)
ggplot2 3.4.1 CRAN (R 4.1.3)
ggpubr 0.4.0 CRAN (R 4.1.2)
ggridges 0.5.4 CRAN (R 4.1.3)
ggsignif 0.6.4 CRAN (R 4.1.3)
globals 0.16.1 CRAN (R 4.1.3)
glue 1.6.2 CRAN (R 4.1.3)
googledrive 2.0.0 CRAN (R 4.1.2)
googlesheets4 1.0.1 CRAN (R 4.1.3)
gower 1.0.0 CRAN (R 4.1.2)
gridExtra 2.3 CRAN (R 4.1.2)
gsubfn 0.7 CRAN (R 4.1.2)
gtable 0.3.1 CRAN (R 4.1.3)
hardhat 1.2.0 CRAN (R 4.1.3)
HardyWeinberg 1.7.5 CRAN (R 4.1.3)
haven 2.5.1 CRAN (R 4.1.3)
highr 0.9 CRAN (R 4.1.3)
Hmisc 4.7-1 CRAN (R 4.1.3)
hms 1.1.2 CRAN (R 4.1.3)
htmlTable 2.4.1 CRAN (R 4.1.3)
htmltools 0.5.3 CRAN (R 4.1.3)
htmlwidgets 1.5.4 CRAN (R 4.1.2)
httpuv 1.6.6 CRAN (R 4.1.3)
httr 1.4.4 CRAN (R 4.1.3)
interp 1.1-3 CRAN (R 4.1.3)
ipred 0.9-13 CRAN (R 4.1.3)
IrregLong 0.3.4 CRAN (R 4.1.3)
iterators 1.0.14 CRAN (R 4.1.2)
itertools 0.1-3 CRAN (R 4.1.3)
janitor 2.1.0 CRAN (R 4.1.2)
jpeg 0.1-9 CRAN (R 4.1.1)
jsonlite 1.8.2 CRAN (R 4.1.3)
jtools 2.2.0 CRAN (R 4.1.3)
kableExtra 1.3.4 CRAN (R 4.1.3)
km.ci 0.5-6 CRAN (R 4.1.3)
KMsurv 0.1-5 CRAN (R 4.1.1)
knitr 1.40 CRAN (R 4.1.3)
labeling 0.4.2 CRAN (R 4.1.1)
labelled 2.10.0 CRAN (R 4.1.3)
later 1.3.0 CRAN (R 4.1.2)
lattice 0.20-45 CRAN (R 4.1.1)
latticeExtra 0.6-30 CRAN (R 4.1.3)
lava 1.6.10 CRAN (R 4.1.2)
lazyeval 0.2.2 CRAN (R 4.1.2)
lifecycle 1.0.3 CRAN (R 4.1.3)
listenv 0.8.0 CRAN (R 4.1.2)
lme4 1.1-30 CRAN (R 4.1.3)
lubridate 1.8.0 CRAN (R 4.1.2)
magrittr 2.0.3 CRAN (R 4.1.3)
MASS 7.3-58.1 CRAN (R 4.1.3)
Matrix 1.5-1 CRAN (R 4.1.3)
matrixcalc 1.0-6 CRAN (R 4.1.3)
MatrixModels 0.5-1 CRAN (R 4.1.3)
memoise 2.0.1 CRAN (R 4.1.2)
MEMSS 0.9-3 CRAN (R 4.1.3)
mice 3.14.0 CRAN (R 4.1.2)
mime 0.12 CRAN (R 4.1.1)
miniUI 0.1.1.1 CRAN (R 4.1.3)
minqa 1.2.4 CRAN (R 4.1.2)
missForest 1.5 CRAN (R 4.1.3)
missRanger 2.1.3 CRAN (R 4.1.3)
mitools 2.4 CRAN (R 4.1.2)
mnormt 2.1.0 CRAN (R 4.1.3)
modelr 0.1.9 CRAN (R 4.1.3)
mstate 0.3.2 CRAN (R 4.1.2)
multcomp 1.4-20 CRAN (R 4.1.3)
munsell 0.5.0 CRAN (R 4.1.2)
mvtnorm 1.1-3 CRAN (R 4.1.1)
nlme 3.1-159 CRAN (R 4.1.3)
nloptr 2.0.3 CRAN (R 4.1.3)
nnet 7.3-18 CRAN (R 4.1.3)
officer 0.4.4 CRAN (R 4.1.3)
openxlsx 4.2.5 CRAN (R 4.1.2)
pacman 0.5.1 CRAN (R 4.1.3)
pander 0.6.5 CRAN (R 4.1.3)
parallelly 1.32.1 CRAN (R 4.1.3)
parcats 0.0.5 CRAN (R 4.1.2)
patchwork 1.1.2 CRAN (R 4.1.3)
pillar 1.8.1 CRAN (R 4.1.3)
pkgbuild 1.3.1 CRAN (R 4.1.2)
pkgconfig 2.0.3 CRAN (R 4.1.2)
pkgload 1.3.0 CRAN (R 4.1.3)
plotly 4.10.0 CRAN (R 4.1.2)
plyr 1.8.7 CRAN (R 4.1.3)
png 0.1-7 CRAN (R 4.1.1)
polspline 1.1.20 CRAN (R 4.1.3)
prettyunits 1.1.1 CRAN (R 4.1.2)
processx 3.7.0 CRAN (R 4.1.3)
prodlim 2019.11.13 CRAN (R 4.1.2)
profvis 0.3.7 CRAN (R 4.1.3)
progressr 0.11.0 CRAN (R 4.1.3)
promises 1.2.0.1 CRAN (R 4.1.2)
proto 1.0.0 CRAN (R 4.1.2)
proxy 0.4-27 CRAN (R 4.1.3)
ps 1.7.1 CRAN (R 4.1.3)
psych 2.2.9 CRAN (R 4.1.3)
purrr 0.3.5 CRAN (R 4.1.3)
quantreg 5.94 CRAN (R 4.1.3)
R6 2.5.1 CRAN (R 4.1.3)
ragg 1.2.3 CRAN (R 4.1.3)
randomForest 4.7-1.1 CRAN (R 4.1.3)
ranger 0.14.1 CRAN (R 4.1.3)
RColorBrewer 1.1-3 CRAN (R 4.1.3)
Rcpp 1.0.9 CRAN (R 4.1.3)
readr 2.1.3 CRAN (R 4.1.3)
readxl 1.4.1 CRAN (R 4.1.3)
recipes 1.0.1 CRAN (R 4.1.3)
remotes 2.4.2 CRAN (R 4.1.3)
renv 1.0.1 CRAN (R 4.1.2)
reprex 2.0.2 CRAN (R 4.1.3)
reshape2 1.4.4 CRAN (R 4.1.2)
reticulate 1.26 CRAN (R 4.1.3)
rio 0.5.29 CRAN (R 4.1.2)
rlang 1.0.6 CRAN (R 4.1.3)
rmarkdown 2.16 CRAN (R 4.1.3)
rms 6.3-0 CRAN (R 4.1.3)
rngtools 1.5.2 CRAN (R 4.1.3)
rpart 4.1.16 CRAN (R 4.1.3)
Rsolnp 1.16 CRAN (R 4.1.3)
RSQLite 2.2.18 CRAN (R 4.1.3)
rstatix 0.7.0 CRAN (R 4.1.2)
rstudioapi 0.14 CRAN (R 4.1.3)
rvest 1.0.3 CRAN (R 4.1.3)
sandwich 3.0-2 CRAN (R 4.1.3)
scales 1.2.1 CRAN (R 4.1.3)
sessioninfo 1.2.2 CRAN (R 4.1.2)
shiny 1.7.2 CRAN (R 4.1.3)
snakecase 0.11.0 CRAN (R 4.1.2)
SparseM 1.81 CRAN (R 4.1.1)
sqldf 0.4-11 CRAN (R 4.1.3)
stringi 1.7.6 CRAN (R 4.1.2)
stringr 1.4.1 CRAN (R 4.1.3)
survex 0.2.2 CRAN (R 4.1.2)
survey 4.1-1 CRAN (R 4.1.2)
survival 3.4-0 CRAN (R 4.1.3)
survminer 0.4.9 CRAN (R 4.1.3)
survMisc 0.5.6 CRAN (R 4.1.3)
svglite 2.1.0 CRAN (R 4.1.2)
systemfonts 1.0.4 CRAN (R 4.1.2)
tableone 0.13.2 CRAN (R 4.1.3)
textshaping 0.3.6 CRAN (R 4.1.3)
TH.data 1.1-1 CRAN (R 4.1.3)
tibble 3.1.8 CRAN (R 4.1.3)
tidylog 1.0.2 CRAN (R 4.1.3)
tidyr 1.2.1 CRAN (R 4.1.3)
tidyselect 1.2.0 CRAN (R 4.1.3)
tidyverse 1.3.2 CRAN (R 4.1.3)
timeDate 4021.106 CRAN (R 4.1.3)
truncnorm 1.0-8 CRAN (R 4.1.3)
tzdb 0.3.0 CRAN (R 4.1.3)
urlchecker 1.0.1 CRAN (R 4.1.3)
usethis 2.1.6 CRAN (R 4.1.3)
utf8 1.2.2 CRAN (R 4.1.2)
uuid 1.1-0 CRAN (R 4.1.3)
V8 4.2.1 CRAN (R 4.1.3)
vctrs 0.5.2 CRAN (R 4.1.3)
viridisLite 0.4.1 CRAN (R 4.1.3)
visNetwork 2.1.2 CRAN (R 4.1.3)
webshot 0.5.4 CRAN (R 4.1.3)
withr 2.5.0 CRAN (R 4.1.2)
writexl 1.4.0 CRAN (R 4.1.3)
xfun 0.33 CRAN (R 4.1.3)
xml2 1.3.3 CRAN (R 4.1.2)
xtable 1.8-4 CRAN (R 4.1.2)
yaml 2.3.6 CRAN (R 4.1.3)
zip 2.2.1 CRAN (R 4.1.3)
zoo 1.8-11 CRAN (R 4.1.3)
Code
reticulate::py_list_packages()%>% 
 kable(caption = "Python packages", format = "markdown",
      col.names = c("Package", "Version", "Requirement"),
    row.names = FALSE,
      align = c("c", "l", "r", "r"))
Python packages
Package Version Requirement
asttokens 2.4.1 asttokens==2.4.1
autograd 1.6.2 autograd==1.6.2
autograd-gamma 0.5.0 autograd-gamma==0.5.0
beautifulsoup4 4.12.3 beautifulsoup4==4.12.3
certifi 2023.11.17 certifi==2023.11.17
cffi 1.16.0 cffi==1.16.0
charset-normalizer 3.3.2 charset-normalizer==3.3.2
click 8.1.7 click==8.1.7
cloudpickle 3.0.0 cloudpickle==3.0.0
colorama 0.4.6 colorama==0.4.6
comm 0.2.1 comm==0.2.1
contourpy 1.2.0 contourpy==1.2.0
cycler 0.12.1 cycler==0.12.1
debugpy 1.8.0 debugpy==1.8.0
decorator 5.1.1 decorator==5.1.1
delete-chrome-history-py 0.1.8 delete-chrome-history-py==0.1.8
ecos 2.0.13 ecos==2.0.13
executing 2.0.1 executing==2.0.1
ffmpeg 1.4 ffmpeg==1.4
ffmpeg-python 0.2.0 ffmpeg-python==0.2.0
filedir 0.0.3 filedir==0.0.3
filelock 3.13.1 filelock==3.13.1
fonttools 4.47.2 fonttools==4.47.2
formulaic 1.0.1 formulaic==1.0.1
fsspec 2023.12.2 fsspec==2023.12.2
future 0.18.3 future==0.18.3
gTTS 2.5.1 gTTS==2.5.1
idna 3.6 idna==3.6
interface-meta 1.3.0 interface-meta==1.3.0
ipykernel 6.29.0 ipykernel==6.29.0
ipython 8.20.0 ipython==8.20.0
jedi 0.19.1 jedi==0.19.1
Jinja2 3.1.3 Jinja2==3.1.3
joblib 1.4.0 joblib==1.4.0
jupyter_client 8.6.0 jupyter_client==8.6.0
jupyter_core 5.7.1 jupyter_core==5.7.1
kiwisolver 1.4.5 kiwisolver==1.4.5
lifelines 0.28.0 lifelines==0.28.0
llvmlite 0.41.1 llvmlite==0.41.1
MarkupSafe 2.1.4 MarkupSafe==2.1.4
matplotlib 3.8.2 matplotlib==3.8.2
matplotlib-inline 0.1.6 matplotlib-inline==0.1.6
more-itertools 10.2.0 more-itertools==10.2.0
mpmath 1.3.0 mpmath==1.3.0
multipledispatch 1.0.0 multipledispatch==1.0.0
natsort 8.4.0 natsort==8.4.0
nest-asyncio 1.5.9 nest-asyncio==1.5.9
networkx 3.2.1 networkx==3.2.1
numba 0.58.1 numba==0.58.1
numexpr 2.10.0 numexpr==2.10.0
numpy 1.26.3 numpy==1.26.3
openai-whisper 20231117 openai-whisper==20231117
osqp 0.6.5 osqp==0.6.5
packaging 23.2 packaging==23.2
pandas 2.2.0 pandas==2.2.0
pandas-flavor 0.6.0 pandas-flavor==0.6.0
parso 0.8.3 parso==0.8.3
patsy 0.5.6 patsy==0.5.6
pillow 10.2.0 pillow==10.2.0
platformdirs 4.1.0 platformdirs==4.1.0
prompt-toolkit 3.0.43 prompt-toolkit==3.0.43
psutil 5.9.8 psutil==5.9.8
pure-eval 0.2.2 pure-eval==0.2.2
pyarrow 15.0.0 pyarrow==15.0.0
pycparser 2.22 pycparser==2.22
pydub 0.25.1 pydub==0.25.1
Pygments 2.17.2 Pygments==2.17.2
pyjanitor 0.26.0 pyjanitor==0.26.0
pyparsing 3.1.1 pyparsing==3.1.1
pyreadr 0.5.0 pyreadr==0.5.0
python-dateutil 2.8.2 python-dateutil==2.8.2
pytz 2023.3.post1 pytz==2023.3.post1
pywin32 306 pywin32==306
pyzmq 25.1.2 pyzmq==25.1.2
qdldl 0.1.7.post1 qdldl==0.1.7.post1
regex 2023.12.25 regex==2023.12.25
requests 2.31.0 requests==2.31.0
rpy2 3.5.16 rpy2==3.5.16
scikit-learn 1.3.2 scikit-learn==1.3.2
scikit-survival 0.22.2 scikit-survival==0.22.2
scipy 1.11.4 scipy==1.11.4
semantic-version 2.10.0 semantic-version==2.10.0
setuptools-rust 1.8.1 setuptools-rust==1.8.1
six 1.16.0 six==1.16.0
soupsieve 2.5 soupsieve==2.5
SpeechRecognition 3.10.1 SpeechRecognition==3.10.1
spyder-kernels 2.5.0 spyder-kernels==2.5.0
stack-data 0.6.3 stack-data==0.6.3
statsmodels 0.14.1 statsmodels==0.14.1
sympy 1.12 sympy==1.12
target 0.0.11 target==0.0.11
threadpoolctl 3.4.0 threadpoolctl==3.4.0
tiktoken 0.5.2 tiktoken==0.5.2
torch 2.1.2 torch==2.1.2
tornado 6.4 tornado==6.4
tqdm 4.66.1 tqdm==4.66.1
traitlets 5.14.1 traitlets==5.14.1
translator 0.0.9 translator==0.0.9
typing_extensions 4.9.0 typing_extensions==4.9.0
tzdata 2023.4 tzdata==2023.4
tzlocal 5.2 tzlocal==5.2
urllib3 2.1.0 urllib3==2.1.0
watchdog 3.0.0 watchdog==3.0.0
wcwidth 0.2.13 wcwidth==0.2.13
whisper 1.1.10 whisper==1.1.10
wrapt 1.16.0 wrapt==1.16.0
xarray 2024.1.1 xarray==2024.1.1

Save

Code
folder_path <- ifelse(dir.exists("E:/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/"),
                      "E:/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/",
                      "C:/Users/CISS Fondecyt/Mi unidad/Alvacast/SISTRAT 2022 (github)/_proposal_grant/2023/")
save.image(paste0(folder_path,"an_grant_23_24_3.RData"))